vb脚本中的日期问题 [英] Date issue in vb script

查看:11
本文介绍了vb脚本中的日期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是当我检索日期时,它以这种格式提供给我:

My issue is when I retrieve date back it gives me in this format:

lastseenstatus=rsprefobj("lastseentstamp")

19-07-2014 15:31:32

我想要 7/19/2014 3:31:32 PM 格式,AM/PM 完整.

I want it in 7/19/2014 3:31:32 PM format with AM/PM intact.

请帮忙..

推荐答案

首先需要确定rsprefobj("lastsentstamp")的数据类型:

MsgBox TypeName(rsprefobj("lastseentstamp"))

如果是字符串,需要先转换成日期时间值:

If it's a string, you need to convert it to a datetime value first:

lastseenstatus = CDate(rsprefobj("lastseentstamp"))

如果您希望根据系统的区域设置格式化日期,请使用 FormatDateTime() 作用为 @John建议:

If you want the date formatted according to the system's regional settings, use the FormatDateTime() function as @John suggested:

MsgBox FormatDateTime(lastseenstatus)

如果无论系统的区域设置如何,您都需要不同的日期格式,您必须自己构建格式化字符串:

If you need a distinct date format regardless of the system's regional settings you have to either build the formatted string yourself:

Function LPad(v) : LPad = Right("00" & v, 2) : End Function

Function FormatDate(d)
  formattedDate = Month(d) & "/" & LPad(Day(d)) & "/" & Year(d) & " " & _
                  ((Hour(d) + 23) Mod 12 + 1) & ":" & LPad(Minute(d)) & ":" & _
                  LPad(Second(d))

  If Hour(d) < 12 Then
    formattedDate = formattedDate & " AM"
  Else
    formattedDate = formattedDate & " PM"
  End If

  FormatDate = formattedDate
End Function

MsgBox FormatDate(lastseenstatus)

或使用 .Net StringBuilder 类:

or use the .Net StringBuilder class:

Set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat "{0:M/dd/yyyy h:mm:ss tt}", lastseenstatus

MsgBox sb.ToString()

不过,在我的测试中,我无法让 tt 格式说明符起作用,因此您可能不得不求助于这样的方法:

In my tests I wasn't able to get the tt format specifier to work, though, so you may have to resort to something like this:

Set sb = CreateObject("System.Text.StringBuilder")

If Hour(lastseenstatus) < 12 Then
  am_pm = "AM"
Else
  am_pm = "PM"
End If

sb.AppendFormat_5 Nothing, "{0:M/dd/yyyy h:mm:ss} {1}", _
  Array(lastseenstatus, am_pm)

MsgBox sb.ToString()

这篇关于vb脚本中的日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆