如何在vb.net中转换日期格式? [英] How to convert date format in vb.net?

查看:274
本文介绍了如何在vb.net中转换日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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

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字符串值转换为适当的 DateTime 变量,Net有很多方法:

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

' 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")) 

DateTime 类型是一个>值;他们没有格式。格式是我们如何向人类显示数据(如上面的 .ToString()),以及我们如何告诉 DataTime 将文本数据从人类解析为 DateTime 变量时,期望的模式。

DateTime types 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 DataTime the pattern to expect when parsing text data from humans into a DateTime variable.

使用许多VB的功能。有些不会创建日期类型,只是新的字符串变量。当使用来自其他文化的日期字符串时, CDate 可能特别有问题。它假定字符串是当前的文化格式,可能不是这种情况。这可能导致 08/07 / yyyy 转换为 07/08 / yyyy

You must be careful when using many of the VB functions. Some do not create date types at all, just new string variables. CDate can 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/yyyy converting to 07/08/yyyy.

从原始问题:

我得到xml响应日期格式字符串是MM / dd / yyyy h:mm:ss a

发表评论:

xml返回日期格式是7/8/2014 12:00:00 PM

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

The format specified in the question does not match the example posted in the comment. The xmlDate text is in fact in M/d/yyyy format, not MM/dd/yyyy! Using ParseExact means we are giving DateTime the exact format to expect. When the format does not match the actual string pattern, it will fail:

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)

这将失败,因为文本不在 MM / dd 格式。请注意,M / d 可以MM / dd因为几天和几个月将是2个字符(10/20 ...)。但反之亦然:MM / dd将需要领先的 0 。指定正确的格式,你不会得到格式异常:

This will fail because the text is not in MM/dd format. Note that "M/d" can parse 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:

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









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



ParseExact is probably the best approach here, because it appears you are importing data from elsewhere. For simple data validation of user input, Parse or TryParse are usually enough. These will try to parse the text using any of the format patterns defined for the current culture.

一些文化有100多个。这意味着用户几乎可以输入日期数据,而且您的代码仍然可以解析/转换为 DateTime 类型。

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 DateTime type.

请参阅 DateTime.ParseExact 了解更多信息。

See DateTime.ParseExact for more information.

这篇关于如何在vb.net中转换日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆