• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    VBS日期(时间)格式化函数代码

    发布者: 涵韵3588 | 发布时间: 2025-8-14 04:31| 查看数: 106| 评论数: 0|帖子模式

    核心代码
    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. WScript.Echo currentTimeStr1 '2019-04-11 15:57:57
    4. WScript.Echo currentTimeStr2 '2019-04-11

    5. '格式化时间方法 n_Flag(1-5)
    6. WScript.Echo Format_Time(Now(),5)

    7. Function Format_Time(s_Time, n_Flag)
    8.         Dim y, m, d, h, mi, s
    9.         Format_Time = ""
    10.         If IsDate(s_Time) = False Then Exit Function
    11.         y = cstr(year(s_Time))
    12.         m = cstr(month(s_Time))
    13.         If len(m) = 1 Then m = "0" & m
    14.         d = cstr(day(s_Time))
    15.         If len(d) = 1 Then d = "0" & d
    16.         h = cstr(hour(s_Time))
    17.         If len(h) = 1 Then h = "0" & h
    18.         mi = cstr(minute(s_Time))
    19.         If len(mi) = 1 Then mi = "0" & mi
    20.         s = cstr(second(s_Time))
    21.         If len(s) = 1 Then s = "0" & s
    22.         Select Case n_Flag
    23.                 Case 1
    24.                         ' yyyy-mm-dd hh:mm:ss
    25.                         Format_Time = y & "-" & m & "-" & d & " "& h &":" & mi &":" & s
    26.                 Case 2
    27.                         ' yyyy-mm-dd
    28.                         Format_Time = y & "-" & m & "-" & d
    29.                 Case 3
    30.                         ' hh:mm:ss
    31.                         Format_Time = h & ":" & mi & ":" & s
    32.                 Case 4
    33.                         ' yyyy年mm月dd日
    34.                         Format_Time = y & "年" & m & "月" & d & "日"
    35.                 Case 5
    36.                         ' yyyymmdd
    37.                         Format_Time = y & m & d
    38.         End Select
    39. 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. '转化日期,将 一位补上零  2003-1-2  -->  2003-01-02
    10. function ConvertDate(tDate)
    11.    ttt=tDate
    12.    if isdate(tDate) then
    13.       ttt=year(tDate) & "-" & FillZero(month(tDate)) & "-" & FillZero(day(tDate))
    14.    end if
    15.    ConvertDate=ttt
    16. end function

    17. '输入一个日期时间串,转换成年四位,其他两位的新的日期时间串
    18. function ConvertDateTime(tDateTime)
    19.    ttt=tDateTime
    20.    if isdate(tDateTime) then
    21.       ttt=year(tDateTime) & "-" & FillZero(month(tDateTime)) & "-" & FillZero(day(tDateTime)) & " " & FillZero(cstr(hour(tDateTime))) & ":" & FillZero(cstr(minute(tDateTime))) & ":" & FillZero(cstr(second(tDateTime)))
    22.    end if
    23.    ConvertDateTime=ttt
    24. end function
    复制代码
    这篇文章就介绍到这了,需要的朋友可以参考一下。

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表