日期问题,VB脚本 [英] Date issue in vb script

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

问题描述

我的问题是,当我取回追溯到它给我的格式为:

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

lastseenstatus=rsprefobj("lastseentstamp")

19-07-2014 15:31:32

我想在 2014年7月19日下午3时31分32秒与格式 AM / PM 完好

请帮助..

推荐答案

首先,你需要确定 RS prefobj的数据类型(lastseentstamp)

MsgBox TypeName(rsprefobj("lastseentstamp"))

如果它是一个字符串,你需要将其转换为datetime值第一:

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

lastseenstatus = CDate(rsprefobj("lastseentstamp"))

如果您想根据系统的区域设置格式的日期,使用的 的FormatDateTime() 功能 @约翰建议:

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天全站免登陆