经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP/ADO/VBScript » 查看文章
VBS日期(时间)格式化函数代码
来源:jb51  时间:2019/10/21 8:37:57  对本文有异议

核心代码

  1. currentTimeStr1 = CStr(Year(Now()))&"-"&Right("0"&Month(Now()),2)&"-"&Right("0"&Day(Now()),2)&" "&Right("0"&Hour(Now()),2)&":"&Right("0"&Minute(Now()),2)&":"&Right("0"&Minute(Now()),2)
  2. currentTimeStr2 = CStr(Year(Now()))&"-"&Right("0"&Month(Now()),2)&"-"&Right("0"&Day(Now()),2)
  3.  
  4. WScript.Echo currentTimeStr1 '2019-04-11 15:57:57
  5. WScript.Echo currentTimeStr2 '2019-04-11
  6.  
  7. '格式化时间方法 n_Flag(1-5)
  8. WScript.Echo Format_Time(Now(),5)
  9.  
  10. Function Format_Time(s_Time, n_Flag)
  11. Dim y, m, d, h, mi, s
  12. Format_Time = ""
  13. If IsDate(s_Time) = False Then Exit Function
  14. y = cstr(year(s_Time))
  15. m = cstr(month(s_Time))
  16. If len(m) = 1 Then m = "0" & m
  17. d = cstr(day(s_Time))
  18. If len(d) = 1 Then d = "0" & d
  19. h = cstr(hour(s_Time))
  20. If len(h) = 1 Then h = "0" & h
  21. mi = cstr(minute(s_Time))
  22. If len(mi) = 1 Then mi = "0" & mi
  23. s = cstr(second(s_Time))
  24. If len(s) = 1 Then s = "0" & s
  25. Select Case n_Flag
  26. Case 1
  27. ' yyyy-mm-dd hh:mm:ss
  28. Format_Time = y & "-" & m & "-" & d & " "& h &":" & mi &":" & s
  29. Case 2
  30. ' yyyy-mm-dd
  31. Format_Time = y & "-" & m & "-" & d
  32. Case 3
  33. ' hh:mm:ss
  34. Format_Time = h & ":" & mi & ":" & s
  35. Case 4
  36. ' yyyy年mm月dd日
  37. Format_Time = y & "年" & m & "月" & d & "日"
  38. Case 5
  39. ' yyyymmdd
  40. Format_Time = y & m & d
  41. End Select
  42. End Function

vbscript下格式化时间和日期的函数

我们有时候遇到的日期格式可能是2020-1-12   ,系统自动将月份中的0去掉了,但是有时候我们需要完整的日期格式 ,如:2020-01-12  那么怎么办呢?下面的几个函数可以轻松搞定

  1. '将一个一位的数字前面加零
  2. function FillZero(str)
  3.    ttt=str
  4.    if len(str)=1 then
  5.       ttt="0" & str
  6.    end if
  7.    FillZero=ttt
  8. end function
  9.  
  10. '转化日期,将 一位补上零  2003-1-2  -->  2003-01-02
  11. function ConvertDate(tDate)
  12.    ttt=tDate
  13.    if isdate(tDate) then
  14.       ttt=year(tDate) & "-" & FillZero(month(tDate)) & "-" & FillZero(day(tDate))
  15.    end if
  16.    ConvertDate=ttt
  17. end function
  18.  
  19. '输入一个日期时间串,转换成年四位,其他两位的新的日期时间串
  20. function ConvertDateTime(tDateTime)
  21.    ttt=tDateTime
  22.    if isdate(tDateTime) then
  23.       ttt=year(tDateTime) & "-" & FillZero(month(tDateTime)) & "-" & FillZero(day(tDateTime)) & " " & FillZero(cstr(hour(tDateTime))) & ":" & FillZero(cstr(minute(tDateTime))) & ":" & FillZero(cstr(second(tDateTime)))
  24.    end if
  25.    ConvertDateTime=ttt
  26. end function

这篇文章就介绍到这了,需要的朋友可以参考一下。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号