Convert Time and Date in string to date VB.net - 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

Related

Convert String to Timestamp in Spark/Hive

I need to convert string to Timestamp.
The problem is that the input is coming from a csv file and contains date-time values such as:
Mar 3 2022 8:30AM
Apr 27 2022 7:37AM
If I use the following conversion:
to_timestamp(to_timestamp(trim(DateColumn), 'MMM dd yyyy h:mma'), 'yyyy-MM-dd HH:mm:ss')
It converts the date Apr 27 2022 7:37AM correctly, but throws error while converting Mar 3 2022 8:30AM because of the extra space between the Month and Date values and that the date 3 is not 03.
Is there a way to convert these 2 strings formats into Datetime?
It is recommended that you first uniformly replace multiple spaces with a single space, and then convert to timestamp.
val df1 = df.withColumn("ts", to_timestamp(regexp_replace(trim(col("ts")), "\\s+", " "), "MMM d y h:mma"))

How to convert "Wed Jan 26 2022" to date in sql?

I have varchar like this "Wed Jan 26 2022"
I need to convert this to date in sql. How can i do this
for Sql Server:
convert(date, substring('Wed Jan 26 2022',5,11),9)
we ignore the Day name (superfluous), and convert the rest using format 9 indicating Mon dd yyyy format.
SQL*plus server (Here's my code) -
If I am right, you want such type of string which is an invalid one to convert that into a valid one so that you can store valid data into the database. then this code you can use->
SELECT TO_DATE('WED JAN 26 2022','DY MON DD YYYY')FROM DUAL;
(Explanation)->
Code will convert invalid date datatype to a valid date data type which is used in Oracle(SQL).
DY = Abbreviated Week Day
DD = Month day indicator
MON = Abbreviated month
YYYY = Four-digit year indicator

How to covert date format in react native?

I am woking on react native project want convert date as below
2020-04-12 06:02:00
To
Sun Apr 12 2020 06:02:00 GMT+0530 (IST)
I tried with moment as below
let momentObj = moment(resultFltLeg.estArrTime)
let showDate = moment(momentObj).format("EEE MMM dd yyyy HH:mm:ss 'GMT'z")
but getting below result
777 Apr Su 2020 06:02:00 'G4T'
Please suggest way to convert date in required format.
2.
After conversion i want to assign this date to other date with same format as below
myFltLeg.estArrTime = New Date(resultFltLeg.estArrTime);
Please let me know if is this correct way of assign one date to other date
myFltLeg.estArrTime // Mon Oct 12 2020 16:42:00 GMT+0530 (IST)
Try this :
const form = someday.format("ddd MMM D YYYY, hh:mm:ss ")
this will give you everything until the GMT - for that I think you need moment-timezone because the option to append z or zz seems deprecated.

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