string VB 脚本日期格式“YYYYMMDDHHMMSS”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28765980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:21:37  来源:igfitidea点击:

VB Script date formats "YYYYMMDDHHMMSS"

stringdatevbscriptformats

提问by Mike Harris

As title surgest I need to fomat the now () function to display on the format "YYYYMMDDHHMMSS"

作为标题激增,我需要格式化 now () 函数以显示格式“YYYYMMDDHHMMSS”

I did have a play about trying to split it out but this drops leading zeros that I need to retain

我确实尝试过将其拆分,但这会降低我需要保留的前导零

example below mydt was "27/02/2015 13:02:27"

mydt 下面的例子是“27/02/2015 13:02:27”

mydt = now() 

MSGBOX Year(mydt)& Month(mydt)& Day(mydt)& Hour(mydt)& Minute(mydt)& second(mydt)

this returns "201522713227"

这将返回“201522713227”

i need it to return "20150227130227" i could use a if < 10 but there must be a slicker way

我需要它返回“2015 022713 0227”我可以使用 if < 10 但必须有一种更灵活的方式

回答by Mike Harris

Thanks to @Ekkehard.Horner and @Bagger

感谢@Ekkehard.Horner 和@Bagger

I have reviewed your advice and have chosen to go with the below, adapted for my needs.

我已经查看了您的建议,并选择了以下建议,以满足我的需求。

I have chosen this one as it is a lot more useable/adaptable I can swap and change date formats as required.

我选择了这个,因为它更有用/适应性更强,我可以根据需要交换和更改日期格式。

Dim g_oSB : Set g_oSB = CreateObject("System.Text.StringBuilder")

Function sprintf(sFmt, aData)
   g_oSB.AppendFormat_4 sFmt, (aData)
   sprintf = g_oSB.ToString()
   g_oSB.Length = 0
End Function

'-------------------------------------------------------------------

Dim dt : dt = now()

WScript.Echo sprintf("{0:yyyyMMddhhmmss}", Array(dt))

This returns the value in required format yyyyMMddhhmmss

这将返回所需格式的值 yyyyMMddhhmmss

20150302110727

If you just require date you would simply change the sprintf

如果您只需要日期,您只需更改 sprintf

sprintf("{0:yyyyMMdd}", Array(dt))

Just want the time

只想要时间

sprintf("{0:hhmmss}", Array(dt))

and so on.....

等等.....

回答by Gil Allen

Here is my full method I used to make it nice. Good for parsing the data for SQL.

这是我用来使它变得更好的完整方法。适合解析 SQL 数据。

function getDateFormatedWithDash(DateVal)
    rtnDateStr = year(DateVal)
    m=month(DateVal)
    d=day(DateVal)
    h=Hour(DateVal)
    Min=Minute(DateVal)
    sec=second(DateVal)

    if month(DateVal)<10 then
        m="0"&month(DateVal)
    end if

    if day(DateVal)<10 then
        d="0"&day(DateVal)
    end if

    if Hour(DateVal)<10 then
        h="0"&Hour(DateVal)
    end if  

    if Minute(DateVal)<10 then
        Min="0"&Minute(DateVal)
    end if  

    if second(DateVal)<10 then
        sec="0"&second(DateVal)
    end if


    rtnDateStr = rtnDateStr&"-"&m&"-"&d&" "&h&":"&Min&":"&sec
    getDateFormatedWithDash=rtnDateStr
end function

回答by dta_phx

Old post but was looking and stumbled on this. Ended up going with the following example that can also be used as a one-liner...

旧帖子,但正在寻找并偶然发现了这一点。最终使用以下示例,该示例也可以用作单行...

wscript.echo DateString(now())

Function DateString(dDate)
    DateString = Year(dDate)& right("0" & Month(dDate),2) & right("0" & Day(dDate),2) & right("0" & Hour(dDate),2) & right("0" & Minute(dDate),2) & right("0" & second(dDate),2)
End Function

回答by Bagger

This works, you could also use regex

这有效,您也可以使用正则表达式

mydt = now() 
wscript.echo (Month(mydt))
mm = add0( Month(mydt))
dd = add0( Day(mydt))
hh = add0( Hour(mydt))
mn = add0( Minute(mydt))
ss = add0( second(mydt))

MSGBOX Year(mydt)& mm & dd & hh & mn & ss

Function add0 (testIn)
 Select Case Len(testIn) < 2
   CASE TRUE
     add0 = "0" & testIn
   Case Else
     add0 = testIn
  End Select      
End Function    

回答by Bagger

Here is a regex example

这是一个正则表达式示例

  mydt = now()
Set regEx = New RegExp
  With regEx
  .Pattern = "\b\d\b"
  .IgnoreCase = True
  .Global = True
 End With

wscript.echo  Year(mydt)& _
  regEx.Replace(Month(mydt),"0" Month(mydt)) & _
  regEx.Replace(Day(mydt),"0" & Day(mydt)) &_
  regEx.Replace(Hour(mydt),"0" & Hour(mydt)) &_
  regEx.Replace(Minute(mydt),"0" & Minute(mydt)) & _ 
  regEx.Replace(second(mydt),"0" & second(mydt))

Set RegularExpressionObject = nothing