How to parse DateTime from string - vb.net

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"

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

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.

Convert String to DateTime in VB

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.

Conditional DateTime.ParseExact format

I am parsing a date from a bank deposit report, and the format is like this:
Jul 9 2015
Jun 20 2015
Basically MMM dd yyyy except that the single digit day does not contain a leading zero. Is there a simple way to do conditional formatting in DateTime.ParseExact()? Or will I have to pre-process the date string and either add the leading zero or remove the extra space?? Here is what works for the single digit day dates:
Dim dtDepositDate As DateTime
dtDepositDate = DateTime.ParseExact(strDate, "MMM d yyyy", CultureInfo.InvariantCulture)
and obviously, MMM dd yyyy would work for the two digit dates, but would not work for the single digit dates with an extra space in between.
For single/double digit day part
Use single d which is good for both single and double digits day value.
Having single d would effect the values if the DateTime is converted to string. As far as parsing is concerned, it will work for both single and double digits day values, like 01 , 1 , 11, 20 etc. The same is true for M, H, m, specifier for Month, Hour Minutes etc.
For multiple spaces
For multiple spaces use DateTimeStyles.AllowWhiteSpaces in parsing.
DateTime dt = DateTime.ParseExact("Jul 9 2015", "MMM d yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces);
or for double digit day part:
DateTime dt = DateTime.ParseExact("Jun 20 2015", "MMM d yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces);

Text to date format in sql

How can i convert the table containing date in text format DY Mon DD YYYY into DD MM YY date format which can be used for sorting when exported to a spread sheet.
The first date format is "Sun Jan 6 2013" which needs to be converted to "06-Jan-13" to be in date format not text which can be sorted on spread sheet and the type of the column is Text.
Thanks in advance.
You can convert the text you have to a DATE value, and then re-format it:
SELECT TO_CHAR(TO_DATE(date_column, 'DY MON DD YYYY'), 'DD MM YY')
FROM my_table