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
I have a need to convert dates of format Fri Jun 30 '17, 11:15:56 am to mm/dd/yyyy format using a VBA. I tried format() function but it didn't work.
If format() doesn't work, then (as you haven't told us otherwise) we have to assume that the cell contains that date as text, not as a date.
In which case, allow me to offer one of many methods of converting this to mm/dd/yyyy:
Format(DateValue(Replace(Mid(Split(Range("A1").Value, ", ")(0), 4, 20), "'", "")), "mm/dd/yyyy")
As #Peh mentions though, this is not the best way to deal with a date. It is better to hold the date as a Date - and if you're writing to a cell, to write that Date to the cell as a Date. This makes manipulation of the date much easier and you just apply formatting to the cell to get the desired look.
To get the Date, you could use:
DateValue(Replace(Mid(Split(Range("A1").Value, ", ")(0), 4, 20), "'", ""))
Explanation:
Split(Range("A1").Value, ", ")(0)
This creates an array that contains the string split in two, the first part (0) contains the left element, the second part (1) contains the right element.
So Fri Jun 30 '17, 11:15:56 am becomes Fri Jun 30 '17
We then take a portion of the left hand part above:
Mid(<left hand part>, 4, 20)
from character 4, for (up to) 20 characters.
So Fri Jun 30 '17 becomes Jun 30 '17
We then remove the ' using a Replace(text , "'", "") function.
So Jun 30 '17 becomes Jun 30 17
The DateValue function converts strings that contain dates into an actual date value. Jun 30 17 is recognisable to Excel as a date and given that the month element is obvious (i.e. a word and not a number) then Excel is able to un-ambiguously convert it.
So Jun 30 17 becomes 42916 (- which is 42,916 days since 31st Dec 1899).
I am trying to build a custom date format that matches the format below. I've included an example of the number value (date) and how the output should look. I've tried something like: ddd mmm d, yyyy h:mm:ssAM/PM [ddd mmm d, yyyy h:mm:ssAM/PM] as a custom format, but can't seem to get it to work. Can you tell me where I am going wrong? Thank you in advance!
(Date time) 1546926706 = (Formatted Output) Mon Jan 7, 2019 9:51:46pm PST [Mon Jan 7, 2019 10:51:46pm]
Attempt at translating format into custom format Excel understands:
`
ddd mmm d, yyyy h:mm:ssAM/PM [ddd mmm d, yyyy h:mm:ssAM/PM]
Basically I'm trying to reverse engineer a custom date format because I can't access the system that is sending me the data and need it to make sense on for users on the front end.
`
1546926706 is UNIX/POSIX time where each 1 is a second after at 1-Jan-1970. You need a conversion to an excel datetime where each 1 is a day that starts 1-Jan-1900.
'Tue Jan 8, 2019 5:51:46 AM PST
=TEXT(1546926706/86400 + 25569, "ddd mmm d, yyyy h:mm:ss am/pm \P\S\T")
That's returns text, not a true datetime but it shows you the conversion and format mask.
Is there any way to automatically convert PST Date in to IST date. When I put date in column E and F I have dates in PST time zone. I want to convert in IST time zone. after i want output in column G. the difference between two dates.
I have solved this with excel formula but I want this to done with VBA.
For eg.
> E F G
1/14/2016 2:49:00 PM 1/14/2016 2:57:00 PM 0:08
1/14/2016 2:45:00 PM 1/14/2016 2:59:00 PM 0:09
Consider:
Sub qwerty()
Dim pst As Date, ist As Date
pst = DateSerial(2016, 1, 14) + TimeSerial(14, 49, 0)
ist = pst + TimeSerial(13, 30, 0)
MsgBox pst & vbCrLf & ist
End Sub
String string = "December 16, 2016 12:10:05 PST";
DateFormat format = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss z", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
Output : Sat Dec 17 01:40:05 IST 2016
I have an RFC1123 Date string, such as: 'Mon, 01 Jan 0001 00:00:01 GMT'
I want to parse this string into a Date in NodeJS. I assumed that new Date('Mon, 01 Jan 0001 00:00:01 GMT') would achieve what I wanted... but it doesn't. When I inspect the content of the resultant Date I see that .toUTCString() gives Mon, 01 Jan 2001 00:00:01 GMT.
I am at a loss as to how the parsing function determined that the year was 2001.
What's even more bizarre is that if I use ISO8601 format instead, and do: new Date('0001-01-01T00:00:01Z').toUTCString() I get Mon, 01 Jan 1 00:00:01 GMT -- exactly what I would expect -- yet if I then do new Date('Mon, 01 Jan 1 00:00:01 GMT') I still get the year as 2001.
Note: I can process these same strings in another language (C#) and I get exactly what I would expect.
Anybody have any suggestions for how to correctly deserialize RFC1123 Date's in NodeJS? If the date is "new" (i.e. above the year 1000) things seem to work okay, but "old" dates seem broken, or else I'm missing something "extra" which is required for older dates.
edit: It looks like technically the RFC 1123/2822 format has a lower bound of the year 1900 -- although Date allows us to go below that so I am not sure what's stopping it from supporting years <100.
You could use moment.js to parse the date:
var dateFormat = 'ddd, DD MMM YYYY HH:mm:ss',
dateString = 'Mon, 01 Jan 0001 00:00:01 GMT';
var dateTime = moment.utc(dateString, dateFormat);
The question looks little obvious but I hardly found suggestions on this.
I have a date and time as string like below:
string s = "Fri Jun 28 2013 00:00:00 GMT+0530 (India Standard Time)";
DateTime from = Convert.ToDateTime(s).ToUniversalTime();
But this throws the exception:
{System.SystemException}: {"String was not recognized as a valid DateTime."}
Can somebody advise how can I handle this?
You could use the TryParseExact method:
string s = "Fri Jun 28 2013 00:00:00 GMT+0530 (India Standard Time)";
DateTime date;
if (DateTime.TryParseExact(s, "ddd MMM dd yyyy hh:mm:ss 'GMT'zzz '(India Standard Time)'", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
// You could use the date here
}
else
{
// The string was not in the correct format
}