ASP时间函数:标准时间及UNIX时间戳相互转换及时间格式化

把标准时间转换为UNIX时间戳:

'把标准时间转换为UNIX时间戳
Function ToUnixTime(strTime, intTimeZone)
    If IsEmpty(strTime) or Not IsDate(strTime) Then strTime = Now
    If IsEmpty(intTimeZone) or Not isNumeric(intTimeZone) Then intTimeZone = 0
    ToUnixTime = DateAdd("h",-intTimeZone,strTime)
    ToUnixTime = DateDiff("s","1970-01-01 00:00:00", ToUnixTime)
End Function
'示例:ToUnixTime("2009-12-05 12:52:25", +8),返回值为1259988745 
'response.Write ToUnixTime("2009-12-05 12:52:25", +8)

把UNIX时间戳转换为标准时间:

'把UNIX时间戳转换为标准时间
Function FromUnixTime(intTime, intTimeZone)
    If IsEmpty(intTime) or Not IsNumeric(intTime) Then
        FromUnixTime = Now()
        Exit Function
    End If         
    If IsEmpty(intTime) or Not IsNumeric(intTimeZone) Then intTimeZone = 0
    FromUnixTime = DateAdd("s", intTime, "1970-01-01 00:00:00")
    FromUnixTime = DateAdd("h", intTimeZone, FromUnixTime)
End Function
'示例:FromUnixTime("1259988745", +8),返回值2009-12-05 12:52:25 
'response.Write FromUnixTime("1259988745", +8)

多功能日期格式化函数:

<%
'功能:多功能日期格式化函数
'来源:http://jorkin.reallydo.com/article.asp?id=477

Function FormatDate(sDateTime, sReallyDo)
 Dim sJorkin
 sJorkin = GetLocale()
 If Not IsDate(sDateTime) Then sDateTime = Now()
 sDateTime = CDate(sDateTime)
 Select Case UCase(sReallyDo & "")
 Case "0", "1", "2", "3", "4"
  FormatDate = FormatDateTime(sDateTime, sReallyDo)
 Case "00"
  FormatDate = FormatDate(sDateTime, "YYYY-MM-DD hh:mm:ss")
 Case "01"
  FormatDate = FormatDate(sDateTime, "YYYY年MM月DD日")
 Case "02"
  FormatDate = FormatDate(sDateTime, "YYYY-MM-DD")
 Case "03"
  FormatDate = FormatDate(sDateTime, "hh:mm:ss")
 Case "04"
  FormatDate = FormatDate(sDateTime, "hh:mm")
 Case "ISO8601", "GOOGLE", "SITEMAP" '//ISO8601格式, 一般用于GoogleSiteMap, "+08:00" 为时区.
  FormatDate = FormatDate(sDateTime, "YYYY-MM-DDThh:mm:ss.000+08:00")
 Case "RFC822", "RSS", "FEED" '//RFC822格式, 一般用于RSS, "+0800" 为时区.
  SetLocale("en-gb")
  FormatDate = FormatDate(sDateTime, "ew, DD eMM YYYY hh:mm:ss +0800")
  SetLocale(sJorkin)
 Case "RND", "RAND", "RANDOMIZE" '//随机字符串
  Randomize
  sJorkin = Rnd()
  FormatDate = FormatDate(sDateTime, "YYYYMMDDhhmmss") & _
    Fix((9 * 10^6 -1) * sJorkin) + 10^6
 Case Else
  FormatDate = sReallyDo
  FormatDate = Replace(FormatDate, "YYYY", Year(sDateTime))
  FormatDate = Replace(FormatDate, "DD", Right("0" & Day(sDateTime), 2))
  FormatDate = Replace(FormatDate, "hh", Right("0" & Hour(sDateTime), 2))
  FormatDate = Replace(FormatDate, "mm", Right("0" & Minute(sDateTime), 2))
  FormatDate = Replace(FormatDate, "ss", Right("0" & Second(sDateTime), 2))
  FormatDate = Replace(FormatDate, "YY", Right(Year(sDateTime), 2))
  FormatDate = Replace(FormatDate, "D", Day(sDateTime))
  FormatDate = Replace(FormatDate, "h", Hour(sDateTime))
  FormatDate = Replace(FormatDate, "m", Minute(sDateTime))
  FormatDate = Replace(FormatDate, "s", Second(sDateTime))
  If InStr(1, FormatDate, "EW", 1) > 0 Then
  SetLocale("en-gb")
  FormatDate = Replace(FormatDate, "EW", UCase(WeekdayName(Weekday(sDateTime), False)))
  FormatDate = Replace(FormatDate, "eW", WeekdayName(Weekday(sDateTime), False))
  FormatDate = Replace(FormatDate, "Ew", UCase(WeekdayName(Weekday(sDateTime), True)))
  FormatDate = Replace(FormatDate, "ew", WeekdayName(Weekday(sDateTime), True))
  SetLocale(sJorkin)
  Else
  FormatDate = Replace(FormatDate, "W", WeekdayName(Weekday(sDateTime), False))
  FormatDate = Replace(FormatDate, "w", WeekdayName(Weekday(sDateTime), True))
  End If
  If InStr(1, FormatDate, "EMM", 1) > 0 Then
  SetLocale("en-gb")
  FormatDate = Replace(FormatDate, "EMM", MonthName(Month(sDateTime), False))
  FormatDate = Replace(FormatDate, "eMM", MonthName(Month(sDateTime), True))
  SetLocale(sJorkin)
  Else
  FormatDate = Replace(FormatDate, "MM", Right("0" & Month(sDateTime), 2))
  FormatDate = Replace(FormatDate, "M", Month(sDateTime))
  End If
 End Select
End Function

'将日期格式化为ISO8601格式
Response.Write(FormatDate("2008-03-06 08:03:06", "SITEMAP"))
'将日期格式化为RFC822格式
Response.Write(FormatDate("2008-03-06 08:03:06", "RSS"))

'将日期格式化为(英星期, 英月/日/年)
Response.Write(FormatDate(Now(), "eW, EMM/DD/YYYY"))

'将生成一个以年月日时分秒随机数的字符串
Response.Write(FormatDate(Now(), "RND"))
%>
点赞 0 分享
本文为「本站原创」,未经我们许可,严谨任何人或单位以任何形式转载或刊载本文章,我们保留依法追究侵权的权力!

微信联系我们

亿百天技术客户服务

使用微信扫一扫

昵称:亿百天技术

公司:湖北亿百天信息技术有限公司

电话:027-88773336

手机:15342213852

邮箱:serviceebaitian.cn

相关栏目

加载中...

成为会员立享特权

立即注册亿百天公司会员,在线联系客服立即享受会员特权,还有丰厚礼品赠送;签约时可以享受特低折扣优惠,期待您的加入!

客服热线:400-840-3336

我们为您提供个性化的售前咨询服务,及全面的技术售后服务。