如何在 vb.net 中转换日期格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24755940/
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-03 12:46:42  来源:igfitidea点击:

How to convert date format in vb.net?

.netvb.netdatetime

提问by NarasimhaKolla

I am getting xml response date format string is "MM/dd/yyyy h:mm:ss a" but I need convert other date format ""dd MMM yy HH:mm"".

How to convert date format in vb.net? Please give me any suggestion.

我得到 xml 响应日期格式字符串是“MM/dd/yyyy h:mm:ss a”但我需要转换其他日期格式“”dd MMM yy HH:mm””。

如何在 vb.net 中转换日期格式?请给我任何建议。

回答by ??ssa P?ngj?rdenlarp

Assuming you want to convert the xml string value to a proper DateTimevariable, Net has many methods for this:

假设您想将 xml 字符串值转换为适当的DateTime变量,Net 有很多方法可以做到这一点:

' a date value in the string format specified:
Dim xmlDate As String = "07/15/2014 7:07:33 AM"

' create a DATE variable from that string in a known format:
Dim newDate As Date = DateTime.ParseExact(xmlDate, "MM/dd/yyyy h:mm:ss tt",
           Globalization.CultureInfo.InvariantCulture)

Once you have am actual date variable, you can display it in any format required. Doing so does not change the underlying date value, it just changes the output style:

一旦你有了实际的日期变量,你就可以以任何所需的格式显示它。这样做不会更改基础日期值,只会更改输出样式:

Dim myDt As DateTime = DateTime.Now

Console.WriteLine(mydt.ToString("dd MMM yy HH:mm tt"))
Console.WriteLine(mydt.ToString("MM/dd/yyyy h:mm:ss")) 

DateTimetypes are a value; they do not have a format. Formats are for how we display data to humans (as with .ToString()above) and how we tell DataTimethe pattern to expect when parsing text data from humans into a DateTimevariable.

DateTime类型是一个;他们没有格式。格式用于我们如何向人类显示数据(.ToString()如上所述)以及我们如何DataTime在将人类的文本数据解析为DateTime变量时告诉模式预期。

You must be careful when using many of the VB functions. Some do not create date types at all, just new string variables. CDatecan be especially problematic when using date strings from other cultures . It assumes the string is in the current culture format, which may not be the case. This can lead to 08/07/yyyyconverting to 07/08/yyyy.

使用许多 VB 函数时必须小心。有些根本不创建日期类型,只创建新的字符串变量。 CDate使用来自其他文化的日期字符串时可能尤其成问题。它假定字符串采用当前区域性格式,但实际情况可能并非如此。这可能导致08/07/yyyy转换为07/08/yyyy.



From original question:
I am getting xml response date format string is "MM/dd/yyyy h:mm:ss a"

从原始问题:
I am getting xml response date format string is "MM/dd/yyyy h:mm:ss a"

From comment:
xml returning date format is "7/8/2014 12:00:00 PM"

来自评论:
xml returning date format is "7/8/2014 12:00:00 PM"

The format specified in the question does not matchthe example posted in the comment. The xmlDate text is in fact in M/d/yyyyformat, not MM/dd/yyyy! Using ParseExactmeans we are giving DateTimethe exactformat to expect. When the format does notmatch the actual string pattern, it will fail:

问题指定的格式与评论中发布的示例不匹配。xmlDate 文本实际上是M/d/yyyy格式,而不是MM/dd/yyyy! 使用ParseExact手段我们给出DateTime了预期的确切格式。当格式与实际字符串模式匹配时,它将失败:

Dim actualDate As Date
Dim xmlTest As String = "7/8/2014 12:00:00 PM"

actualDate = DateTime.ParseExact(xmlSource, "MM/dd/yyyy h:mm:ss tt",
                                 Globalization.CultureInfo.InvariantCulture)

This will fail because the text is not in MM/ddformat. Note that "M/d"canparse dates from strings in the pattern of "MM/dd"because some days and months will be 2 characters ("10/20..."). But the reverse is not true: "MM/dd" will require the leading 0. Specify the correct format and you wont get a format exception:

这将失败,因为文本MM/dd格式不正确。请注意,"M/d"可以从模式中的字符串解析日期,"MM/dd"因为某些日期和月份将是 2 个字符(“10/20...”)。但反之则不然:“MM/dd”将需要领先的0. 指定正确的格式,您不会得到格式异常:

 actualDate = DateTime.ParseExact(xmlSource, "M/d/yyyy h:mm:ss tt",
                                 Globalization.CultureInfo.InvariantCulture)




ParseExactis probably the best approach here, because it appears you are importing data from elsewhere. For simple data validation of user input, Parseor TryParseare usually enough. These will try to parse the text using any of the format patternsdefined for the current culture.

ParseExact可能是这里最好的方法,因为您似乎是从其他地方导入数据。对于用户输入的简单数据验证,Parse或者TryParse通常就足够了。这些将尝试使用为当前文化定义的任何格式模式来解析文本。

Some cultures have well over 100. This means the user can input date data almost anyway they want and your code can still parse/convert it to a DateTimetype.

有些文化有超过 100 个。这意味着用户几乎可以以任何他们想要的方式输入日期数据,并且您的代码仍然可以将其解析/转换为DateTime类型。

See DateTime.ParseExactfor more information.

有关详细信息,请参阅DateTime.ParseExact

回答by Saleem

Dim theirTime = "07/15/2014 1:43:38 PM"
Dim myFormat = "dd MMM yy HH:mm"

Dim myTime = Format(CDate(theirTime), myFormat)

回答by Nilotpal Basudev Goswami

Dim dat As Date
Dim dd, mm, yyyy As String

DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)

If Len(DateTimePicker1.Value.Day) = 2 Then
    dd = DateTimePicker1.Value.Day
Else
    dd = "0" & DateTimePicker1.Value.Day
End If

If Len(DateTimePicker1.Value.Month) = 2 Then
    mm = DateTimePicker1.Value.Month
Else
    mm = "0" & DateTimePicker1.Value.Month
End If

yyyy = DateTimePicker1.Value.Year

dat = dd & "/" & mm & "/" & yyyy

回答by user10347676

Dim CommenceDateFormat As Date = 
    Date.ParseExact(CommenceDate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo) 'converts date to a format understood for comparisons
Dim EndDateFormat As Date = 
    Date.ParseExact(EndDate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)

回答by Think Different

You can do it like this:

你可以这样做:

Dim time As DateTime = DateTime.Now  'Your date
Dim format As String = "dd MMM yy HH:mm"
Dim newdate = time.ToString(format)