[英]How to convert date format in 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"". 我得到的xml响应日期格式字符串是“MM / dd / yyyy h:mm:ss a”但我需要转换其他日期格式“”dd MMM yy HH:mm“”。
How to convert date format in vb.net? 如何在vb.net中转换日期格式? Please give me any suggestion. 请给我任何建议。
Assuming you want to convert the xml string value to a proper DateTime
variable, 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"))
DateTime
types are a value ; DateTime
类型是一个值 ; 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. 格式是我们如何向人类显示数据(如上面的.ToString()
)以及我们如何告诉DataTime
在将人类的文本数据解析为DateTime
变量时所期望的模式。
You must be careful when using many of the VB functions. 使用许多VB函数时必须小心。 Some do not create date types at all, just new string variables. 有些根本不创建日期类型,只是新的字符串变量。 CDate
can be especially problematic when using date strings from other cultures . 当使用来自其他文化的日期字符串时, CDate
可能特别成问题。 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
. 这可能导致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"
From comment: 来自评论:
xml returning date format is "7/8/2014 12:00:00 PM"
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
! xmlDate文本实际上是M/d/yyyy
格式,而不是MM/dd/yyyy
! Using ParseExact
means we are giving DateTime
the exact format to expect. 使用ParseExact
意味着我们为DateTime
了期望的确切格式。 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)
This will fail because the text is not in MM/dd
format. 这将失败,因为文本不是MM/dd
格式。 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..."). 请注意, "M/d"
可以解析"MM/dd"
模式中字符串的日期,因为某些日期和月份将是2个字符(“10/20 ...”)。 But the reverse is not true: "MM/dd" will require the leading 0
. 但反之则不然:“MM / dd”将要求领先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
is probably the best approach here, because it appears you are importing data from elsewhere. ParseExact
可能是这里最好的方法,因为它似乎是从其他地方导入数据。 For simple data validation of user input, Parse
or TryParse
are usually enough. 对于用户输入的简单数据验证, Parse
或TryParse
通常就足够了。 These will try to parse the text using any of the format patterns defined for the current culture. 这些将尝试使用为当前文化定义的任何格式模式来解析文本。
Some cultures have well over 100 . 有些文化超过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
类型。
See DateTime.ParseExact for more information. 有关更多信息,请参阅DateTime.ParseExact 。
Dim theirTime = "07/15/2014 1:43:38 PM"
Dim myFormat = "dd MMM yy HH:mm"
Dim myTime = Format(CDate(theirTime), myFormat)
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
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)
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.