Need to convert this string:
Mon Oct 31 16:18:15 CDT 2011
Into a valid DateTime value.
Have tried every variation of the date time styles with DateTime.Parseto no avail.
Any ideas?
The problem is with the CDT you have there. This is not a valid portion of a string representing a DateTime.
You may have luck with replacing this with a valid representation of a timezone -0500 and the K format specifier for it.
You can use the following format string to parse the string:
ddd MMM dd HH:mm:ss CDT yyyy
For instance:
DateTime.ParseExact("Mon Oct 31 16:18:15 CDT 2011",
"ddd MMM dd HH:mm:ss CDT yyyy",
CultureInfo.InvariantCulture);
I suggest reading the documentation for Custom Date and Time Format Strings on MSDN.
Related
I'm using RSS feeds from multiple sources and I'm sorting the feeds by date. However, the dates I receive are in different time zones.
This is the format of some of the date strings that I get:
Wed 08 Dec 2021 01:40:31 -0500
Wed 08 Dec 2021 11:11:19 -0600
The "-500' indicates the time zone which is UTC-5. I need to use that "-0500" to convert this string into a date and the date needs to be only in UTC.
Basically from "Wed 08 Dec 2021 01:40:31 -0500" to "12/08/2021 06:40:31"
I've managed to split the string up to sort by date but because of the time difference, it doesn't really help.
Is there coding which I can use to convert the string as-is into date and only UTC time? Or is there just a place where I can start?
Thank you in advance.
Using DateTime.ParseExact, you specify a format to convert from, and that can include "zzz" for the timezone offset. You can also specify that you want the result in UTC:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTime.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing, Globalization.DateTimeStyles.AdjustToUniversal)
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"))
Console.WriteLine(d.Kind.ToString())
Outputs:
2021-12-08 06:40:31
Utc
Of course, format the result as you desire.
Alternatively, if you need to, you can keep the offset by using a DateTimeOffset structure and adjust it to UTC for representation as a string:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTimeOffset.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing)
Console.WriteLine(d.ToUniversalTime.ToString("yyyy-MM-dd HH:mm:ss"))
Outputs:
2021-12-08 06:40:31
How to convert following String to DateTime in VB?
I got error when executing code as below:
Dim date As String = "Wed Mar 30 2016 00:00:00 GMT+0800 (Malay Peninsula Standard Time)"
DateTime.Parse(date)
System.FormatException: "String was not recognized as a valid
DateTime."
Your string is clearly not a standard date and time format for any culture.
But since your string has UTC Offset, I would parse it to DateTimeOffset instead. And since it does not keep timezone name and it's abbreviations, you need to escape them as a string literal delimiter. You can use ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(Malay Peninsula Standard Time)' format for that.
C#
var date = "Wed Mar 30 2016 00:00:00 GMT+0800 (Malay Peninsula Standard Time)";
var dto = DateTimeOffset.ParseExact(date,
"ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(Malay Peninsula Standard Time)'",
CultureInfo.InvariantCulture);
VB.NET
Dim [date] = "Wed Mar 30 2016 00:00:00 GMT+0800 (Malay Peninsula Standard Time)"
Dim dto = DateTimeOffset.ParseExact([date], "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(Malay Peninsula Standard Time)'", CultureInfo.InvariantCulture)
Now you have a DateTimeOffset as {30.03.2016 00:00:00 +08:00}. Now you can use it's .DateTime ot .UtcDateTime properties as you want.
I have a date value (Mon Feb 29 06:01:14 CST 2016) in column. I need to convert this to the format 'YYYY-MM-DD' in hive.
Can any one please help me on this.
There is a unix_timestamp function which should help you. to_date(unix_timestamp(your_column, 'EEE MMM dd HH:mm:ss z yyyy')) This should return a string representing your date.
I need to convert Decimal(17,9) datatype to Timestamp(6).
Example, I am having Decimal(17,9) value as 20150619.154519 and I need to convert it to timestamp like Jun 19, 2015 15:45.
You have a strange format. First convert it to a string, then to a date:
select to_date(to_char(col, '99999999.999999'), 'YYYYMMDD.HHMISS')
Actually, this would probably work without the to_char() part, but it seems safer to do the conversion explicitly.
i am converting my date to Universal Time using mydate.ToUniversalTime() in vb.net its giving me right time but its giving me time in "5/4/2010 4:30:00 AM" format (in vb.net) but i want it in "Wed, 05 May 2010 05:50:00 GMT" format.. please help me to make it in "Wed, 05 May 2010 05:50:00 GMT" format. value of both date are different so please check only their format.
When converting the date to a string, provide a format string.
Dim myDateTime As DateTime = DateTime.Now
Dim myDateTimeString As String = myDateTime.ToString( "ddd, dd MMM yyyy hh:mm:ss ""GMT""" )