string VBScript 将日期转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6792119/
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
VBScript convert date to string
提问by user856354
So I have a possibly simple question that I can not find the answer to.
所以我有一个可能很简单的问题,但我找不到答案。
I am writing a VBScript that will move a subfolder from one folder to another. When moving I want to append the date onto the subfolders name. I have everything working, except I can not figure out how to convert the date to a string so that it can be added to the folder name.
我正在编写一个 VBScript,它将子文件夹从一个文件夹移动到另一个文件夹。移动时,我想将日期附加到子文件夹名称上。我一切正常,只是我不知道如何将日期转换为字符串,以便将其添加到文件夹名称中。
curDate = Month(Date) + "_" + Day(Date) + "_" + Year(Date) + "_" + Time
If fs.FolderExists(rsltFldrPath) Then
'Grab folder and Subfolders
Set fldr = fs.GetFolder(rsltFldrPath)
Set subFldr = fldr.SubFolders
For each folder in subFldr
moveTo = archFldrPath + "\" +folder.name + curDate
fs.MoveFolder folder, moveTo
Next
End If
Any help is appreciated. Thanks!
任何帮助表示赞赏。谢谢!
采纳答案by Ekkehard.Horner
The string concatenation operator in VBScript is "&", so a quick fix would be:
VBScript 中的字符串连接运算符是“&”,因此快速解决方法是:
>> curDate = Month(Date) & "_" & Day(Date) & "_" & Year(Date) & "_" & Time
>> WScript.Echo curDate
>>
7_22_2011_5:32:55 PM
If you specify the exactdesired result, I'm willing to propose a better way to achieve your goal.
如果您指定确切的所需结果,我愿意提出一种更好的方法来实现您的目标。
回答by simon henry
Ekkehard.Hornersanswer won't work as Time needs to be reformatted to remove the colons (:) Folders cannot have colons.
Ekkehard.Horners答案不起作用,因为时间需要重新格式化以删除冒号 (:) 文件夹不能有冒号。
Try;
尝试;
curDate = Month(Date) & "_" & Day(Date) & "_" & Year(Date) & "_" & Time
Replace(CurDate,":","-")
I'm sure there's a more succinct answer but that will work also padding shouldn't be an issue as the underscores will negate duplicate possibilities.
我确信有一个更简洁的答案,但这也行得通,填充也不应该成为问题,因为下划线将否定重复的可能性。
I wanted to comment but don't have enough rep.
我想发表评论,但没有足够的代表。