vba 格式化当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9505408/
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
Formatting the current date
提问by Quintis555
Either I just can't seem to find the right way to word the problem or it's a lot more complicated than I thought, but what is the easiest way to assign the current date to a variable and format it to a specific format? Basically, I want to do the VB.net equivalent of thisVBA line:
要么我似乎无法找到解决问题的正确方法,要么它比我想象的要复杂得多,但是将当前日期分配给变量并将其格式化为特定格式的最简单方法是什么?基本上,我想做这个VBA 行的 VB.net 等价物:
formatted_date = Format(Date, "yyyy-m-d")
Thanks!
谢谢!
回答by John Koerner
Dim formattedDate as String = DateTime.Now.ToString("yyyy-M-d")
Note that capital M is for month, lowercase m would get you minutes.
请注意,大写 M 表示月份,小写 m 表示分钟。
回答by user1140588
If by "Date" you really mean current date (and not an example variable), the VB.NET Equivalent is "Today"
如果“日期”实际上是指当前日期(而不是示例变量),则 VB.NET 等效项是“今天”
Do one of these things... (whichever you like):
做这些事情之一......(无论你喜欢什么):
formatted_date = Today.ToString("yyyy-M-d")
formatted_date = String.Format("{0:yyyy-M-d}", Today)
If your "Date" was just an example variable, just replace "Today" in the above examples with your variable name.
如果您的“日期”只是一个示例变量,只需将上述示例中的“今天”替换为您的变量名称。
回答by HndlCode
to simply : SomeDate.ToString("yyyy-m-d")
简单地说: SomeDate.ToString("yyyy-m-d")
回答by Andrew
Try this
尝试这个
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))