在 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

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

Display date in dd/mm/yyyy format in vb.net

vb.netdatetime-format

提问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.InvariantCultureto ToStringto ensure that /as date separator is used since it would normally be replaced with the current culture's date separator:

您必须传递CultureInfo.InvariantCulturetoToString以确保/使用日期分隔符,因为它通常会被当前文化的日期分隔符替换:

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:

MSDN:“/”自定义格式说明符

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.DateSeparatorproperty of the currentor specified culture.

“/”自定义格式说明符表示日期分隔符,用于区分年、月和日。从当前或指定 区域性DateTimeFormatInfo.DateSeparator属性检索适当的本地化日期分隔符 。

回答by Darshan

Try this.

尝试这个。

 var dateAsString = DateTime.Now.ToString("dd/MM/yyyy");
// dateAsString = "09/07/2013"

and also check this link for more formatting data and time

检查此链接以获取更多格式化数据和时间

回答by matzone

Like this ..

像这样 ..

MsgBox(format(dt,"dd/MM/yyyy"))

回答by VB.NET LEARNER

Dim formattedDate As String = Date.Today.ToString("dd/MM/yyyy")

Check link below

检查下面的链接

回答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"