在 vb.net 中以 dd/mm/yyyy 格式显示日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17546184/
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
Display date in dd/mm/yyyy format in vb.net
提问by Ravikant Upadhyay
I want to display date in 09/07/2013 format instead of 09-jul-13.
我想以 09/07/2013 格式而不是 09-jul-13 格式显示日期。
Dim dt As Date = Date.Today
MsgBox(dt)
回答by Tim Schmelter
First, uppercase MM are months and lowercase mm are minutes.
首先,大写的 MM 是月,小写的 mm 是分钟。
You have to pass CultureInfo.InvariantCulture
to ToString
to ensure that /
as date separator is used since it would normally be replaced with the current culture's date separator:
您必须传递CultureInfo.InvariantCulture
toToString
以确保/
使用日期分隔符,因为它通常会被当前文化的日期分隔符替换:
MsgBox(dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture))
Another option is to escape that custom format specifier by embedding the / within ':
另一种选择是通过在 ' 中嵌入 / 来转义自定义格式说明符:
dt.ToString("dd'/'MM'/'yyyy")
MSDN: The "/" Custom Format Specifier:
The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the
DateTimeFormatInfo.DateSeparator
property of the currentor specified culture.
“/”自定义格式说明符表示日期分隔符,用于区分年、月和日。从当前或指定 区域性的
DateTimeFormatInfo.DateSeparator
属性中检索适当的本地化日期分隔符 。
回答by Darshan
Try this.
尝试这个。
var dateAsString = DateTime.Now.ToString("dd/MM/yyyy");
// dateAsString = "09/07/2013"
回答by matzone
Like this ..
像这样 ..
MsgBox(format(dt,"dd/MM/yyyy"))
回答by VB.NET LEARNER
回答by user5195203
You could decompose the date into it's constituent parts and then concatenate them together like this:
您可以将日期分解为其组成部分,然后像这样将它们连接在一起:
MsgBox(Now.Day & "/" & Now.Month & "/" & Now.Year)
回答by Ken Ince
I found this catered for dates in 21st Century that could be entered as dd/mm or dd/mm/yy. It is intended to print an attendance register and asks for the meeting date to start with.
我发现这适合 21 世纪的日期,可以输入为 dd/mm 或 dd/mm/yy。它旨在打印出勤登记表并要求开始会议日期。
Sub Print_Register()
Dim MeetingDate, Answer
Sheets("Register").Select
Range("A1").Select
GetDate:
MeetingDate = DateValue(InputBox("Enter the date of the meeting." & Chr(13) & _
"Note Format" & Chr(13) & "Format DD/MM/YY or DD/MM", "Meeting Date", , 10000, 10000))
If MeetingDate = "" Then GoTo TheEnd
If MeetingDate < 36526 Then MeetingDate = MeetingDate + 36526
Range("Current_Meeting_Date") = MeetingDate
Answer = MsgBox("Date OK?", 3)
If Answer = 2 Then GoTo TheEnd
If Answer = 7 Then GoTo GetDate
ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)"
TheEnd:
End Sub
回答by Prasanna
if you want to display date along with time when you export to Excel then you can use this
如果要在导出到 Excel 时显示日期和时间,则可以使用它
xlWorkSheet.Cells(nRow, 3).NumberFormat = "dd/mm/yy h:mm AM/PM"