Chinaunix首页 | 论坛 | 博客
  • 博客访问: 11881593
  • 博文数量: 187
  • 博客积分: 7517
  • 博客等级: 少将
  • 技术积分: 1981
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-20 18:51
文章分类

全部博文(187)

文章存档

2015年(3)

2013年(4)

2012年(20)

2011年(2)

2010年(96)

2009年(14)

2008年(47)

2007年(1)

我的朋友

分类: 系统运维

2010-04-17 23:06:11

将下面的代码复制保存到asp中就可以看到效果与调用方法

<%=now()%>



<%=Replace(Now(),":",":")%>





获取当前系统日期和时间,ASP输出可以这样写:<%=now()%>



Year(now()) 获取年份, ASP输出:<%=Year(now())%>



Month(now()) 获取当前月份,ASP输出:<%=Month(now())%>



day(now()) 获取当天数,ASP输出:<%=day(now())%>



Minute(now()) 获取分钟数,ASP输出:<%=Minute(now())%>



Second(now()) 获取秒钟数,ASP输出:<%=Second(now())%>





date() 获取当前系统日期,格式为:2004-2-28



time() 获取当前系统时间,格式为:22:24:59



函数调用格式:FormatDateTime(Date[, NamedFormat])



FormatDateTime函数的语法有以下参数:



参数 描述



Date 必选,要被格式化的日期表达式。



NamedFormat 可选,指示所使用的日期/时间格式的数值,如果省略,则使用vbGeneralDate。





设置



NamedFormat参数可以有以下值:



vbGeneralDate 0 显示日期和/或时间;如果有日期部分,则将该部分显示为短日期格式;如果有时间部



分,则将该部分显示为长时间格式;如果都存在,则显示所有部分。



vbLongDate 1 使用计算机区域设置中指定的长日期格式显示日期。



vbShortDate 2 使用计算机区域设置中指定的短日期格式显示日期。



vbLongTime 3 使用计算机区域设置中指定的时间格式显示时间。



vbShortTime 4 使用24小时格式(hh:mm)显示时间。





示例:



<%

response.write formatdatetime(now,0)&"
"

response.write formatdatetime(now,1)&"
"

response.write formatdatetime(now,2)&"
"

response.write formatdatetime(now,3)&"
"

response.write formatdatetime(now,4)&"
"

%>



显示如下:



2007-9-5 1:52:27



2007年9月5日



2007-9-5



1:52:27



01:52 



<%
'月份转换到中文
Function MonthToCH(TheMonth)
Dim mm
mm=split("一,二,三,四,五,六,七,八,九,十,十一,十二",",")
If IsNumeric(TheMonth) Then
  MonthToCH = mm(TheMonth-1) & "月份"
Else
  MonthToCH = " 月份"
End If
End Function


'月份转换到长英文
Function MonthToLongEN(TheMonth)
Dim mm
mm=split("January,February,March,April,May,June,July,August,September,October,November,December",",")
If IsNumeric(TheMonth) Then
  MonthToLongEN = mm(TheMonth-1)
Else
  MonthToLongEN = " "
End If
End Function


'月份转换到短英文
Function MonthToShortEN(TheMonth)
Dim mm
mm=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",",")
If IsNumeric(TheMonth) Then
  MonthToShortEN = mm(TheMonth-1)
Else
  MonthToShortEN = " "
End If
End Function
%>




获取当前月份,ASP输出:<%=MonthToCH(Month(now()))%>






获取当前月份,ASP输出:<%=MonthToLongEN(Month(now()))%>






获取当前月份,ASP输出:<%=MonthToShortEN(Month(now()))%>



<%
'================================================
   '函数名:FormatDate
   '作 用:格式化日期
   '参 数:DateAndTime   ----原日期和时间
   '        para   ----日期格式
   '返回值:格式化后的日期
   '================================================
 
   Public Function FormatDate(DateAndTime, para)
 
     On Error Resume Next
     Dim y, m, d, h, mi, s, strDateTime
     FormatDate = DateAndTime
     If Not IsNumeric(para) Then Exit Function
     If Not IsDate(DateAndTime) Then Exit Function
     y = CStr(Year(DateAndTime))
     m = CStr(Month(DateAndTime))
     If Len(m) = 1 Then m = "0" & m
     d = CStr(Day(DateAndTime))
     If Len(d) = 1 Then d = "0" & d
     h = CStr(Hour(DateAndTime))
     If Len(h) = 1 Then h = "0" & h
     mi = CStr(Minute(DateAndTime))
     If Len(mi) = 1 Then mi = "0" & mi
     s = CStr(Second(DateAndTime))
     If Len(s) = 1 Then s = "0" & s
    
     Select Case para
 
     Case "1"
    '显示格式:09年07月06日 13:44
    strDateTime = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
 
     Case "2"
    '显示格式:2009-07-06
    strDateTime = y & "-" & m & "-" & d
 
     Case "3"
    '显示格式:2009/07/06
    strDateTime = y & "/" & m & "/" & d
 
     Case "4"
    '显示格式:2009年07月06日
    strDateTime = y & "年" & m & "月" & d & "日"
 
     Case "5"
    '显示格式:07-06 13:45
    strDateTime = m & "-" & d & " " & h & ":" & mi
 
     Case "6"
    '显示格式:07/06
    strDateTime = m & "/" & d
 
     Case "7"
    '显示格式:07月06日
    strDateTime = m & "月" & d & "日"
 
     Case "8"
    '显示格式:2009年07月
    strDateTime = y & "年" & m & "月"
 
     Case "9"
    '显示格式:2009-07
    strDateTime = y & "-" & m
 
     Case "10"
    '显示格式:2009/07
    strDateTime = y & "/" & m
 
     Case "11"
      '显示格式:09年07月06日 13:45
    strDateTime = right(y,2) & "年" &m & "月" & d & "日 " & h & ":" & mi
 
     Case "12"
    '显示格式:09-07-06
    strDateTime = right(y,2) & "-" &m & "-" & d
 
     Case "13"
    '显示格式:07-06
    strDateTime = m & "-" & d
  
     Case "14"
    '显示格式:13:45
    strDateTime = h & ":" & mi
 
     Case Else
 
    strDateTime = DateAndTime
 
     End Select
 
   FormatDate = strDateTime
 
   End Function

%>



获取当前月份,ASP输出:<%=FormatDate(now(),13)%>






获取当前月份,ASP输出:<%=FormatDate(now(),11)%>





把这段代码放入你的Function里就可以直接用了
ASP里格式化日期
复制代码 代码如下:
<%
' ============================================
' 格式化时间(显示)
' 参数:n_Flag
' 1:"yyyy-mm-dd hh:mm:ss"
' 2:"yyyy-mm-dd"
' 3:"hh:mm:ss"
' 4:"yyyy年mm月dd日"
' 5:"yyyymmdd"
' 6:"yyyymmddhhmmss"
' ============================================
Function Format_Time(s_Time, n_Flag)
Dim y, m, d, h, mi, s
Format_Time = ""
If IsDate(s_Time) = False Then Exit Function
y = cstr(year(s_Time))
m = cstr(month(s_Time))
If len(m) = 1 Then m = "0" & m
d = cstr(day(s_Time))
If len(d) = 1 Then d = "0" & d
h = cstr(hour(s_Time))
If len(h) = 1 Then h = "0" & h
mi = cstr(minute(s_Time))
If len(mi) = 1 Then mi = "0" & mi
s = cstr(second(s_Time))
If len(s) = 1 Then s = "0" & s
Select Case n_Flag
Case 1
' yyyy-mm-dd hh:mm:ss
Format_Time = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case 2
' yyyy-mm-dd
Format_Time = y & "-" & m & "-" & d
Case 3
' hh:mm:ss
Format_Time = h & ":" & mi & ":" & s
Case 4
' yyyy年mm月dd日
Format_Time = y & "年" & m & "月" & d & "日"
Case 5
' yyyymmdd
Format_Time = y & m & d
case 6
'yyyymmddhhmmss
format_time= y & m & d & h & mi & s
End Select
End Function
%>

阅读(456) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~