Convert String to DateTime in VB - vb.net

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.

Related

Convert Time and Date in string to date VB.net

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 parse DateTime from string

When reading DateTime from excel, the result day and month are being read as dd MM, while the excel content was according to MM dd style.
S1 contains: "12/09/2017"
The code:
Dim t_from As DateTime
t_from = CDate(s1)
t_from includes Sep as a month, instead of Dec as should be.
I also tried:
Dim b As Boolean = DateTime.TryParseExact(s1, "MM/dd/yy",
System.Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,dt)
this code fails (String was not recognized as a valid DateTime)
How can I convert the text to DateTime VB variable, according to month first (before date)?
Format "M/d/yyyy h:m:s tt" should parse correctly dates from the excel
Dim parsedDate As Date
Date.TryParseExact(
"9/12/2017 12:00:00 AM",
"M/d/yyyy h:m:s tt",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,
parsedDate)
parsedDate.ToString() ' 12.09.2017 00:00:00
For "12/09/2017" where 12 is a month and 09 is a day use format: "MM/dd/yyyy"

VB.net output datetime with timezone and offset

I wish to construct a DateTime which looks like below in Visual Basics:
Wed Aug 23 2017 10:00:00 GMT+0530
Can anyone please suggest how to construct a DateTime info where timezone info could also be shown as above?
You can do something like below...
Dim dt as DateTime = DateTime.UtcNow
Dim output as String = dt.ToLocalTime().ToString("MMM dd, yyyy HH:mm:ss tt ""GMT"" zzz")
Console.WriteLine(output) 'Outputs Aug 23, 2017 11:16:29 AM GMT +01:00
See Custom Date and Time Format Strings for more information.
A DateTime has a Kind which is one of DateTimeKind.Local, DateTimeKind.Utc, or DateTimeKind.Unspecified, so although it has some time zone information, it is too limited to specify another offset.
A DateTimeOffset can hold other offsets, so it may suit your purposes better:
Module Module1
Sub Main()
Dim hereAndNow = DateTime.Now
Dim utc = DateTime.UtcNow
Dim noTzi = Date.SpecifyKind(utc, DateTimeKind.Unspecified)
Dim inIndia As New DateTimeOffset(noTzi, TimeSpan.FromHours(5.5))
Console.WriteLine(hereAndNow.ToString("yyyy-MM-dd HH:mm:ss K"))
Console.WriteLine(utc.ToString("yyyy-MM-dd HH:mm:ss K"))
Console.WriteLine(inIndia.ToString("yyyy-MM-dd HH:mm:ss 'GMT' K"))
Console.ReadLine()
End Sub
End Module
Outputs:
2017-08-23 11:36:55 +01:00
2017-08-23 10:36:55 Z
2017-08-23 10:36:55 GMT +05:30
I had to set the Kind of the DateTime variable utc to DateTimeKind.Unspecified to be able to use it in the DateTimeOffset constructor.
You should use the K custom format specifier for displaying time zone information.

How to convert a string to a valid DateTime

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.

I am converting general date to UniversalTime Please tell me how to get this format

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