Convert uncommon String date/time data to DateTime data type - powershell

I'm trying to implement a PowerShell script to compare DateTime from certificate file(Jave Keystore).
However, the DateTime format that I extract from keystore file is quite complex as example below.
Mon Mar 13 06:40:26 CDT 2023
Sat Sep 18 20:41:47 CDT 2027
It includes time and timezone in the String but I actually need only date like 13-Mar-2023.
Could anyone help suggest how I return this String to be DateTime for comparison?
Thanks a lot.

You can use the [datetime]::ParseExact() method for this:
$dateString = 'Mon Mar 13 06:40:26 CDT 2023'
$date = [datetime]::ParseExact($dateString, 'ddd MMM dd HH:mm:ss CDT yyyy', [cultureinfo]'en-US')
$date.ToString('dd-MMM-yyyy')
Result:
13-Mar-2023
CDT means Central Time Zone (UTC - 6), switched to DayLight Saving Time --> Central Daylight Time (CDT) which is five hours behind UTC

Related

What date format is this : Mon Oct 18 10:26:10 EDT 2021"

What date format is the below and how to get this converted to check with current date in Powershell
Mon Oct 18 10:26:10 EDT 2021
EDT means Eastern Daylight Time (North America), which is 4 hours behind from the UTC universal time.
Using that, you could parse it to your local time by first adding the 4 hours offset to get the date in UTC, and finally using method .ToLocaltime() on it.
[datetime]::ParseExact('Mon Oct 18 10:26:10 EDT 2021', 'ddd MMM dd HH:mm:ss \E\D\T yyyy', [cultureinfo]'en-US').AddHours(4).ToLocaltime()
On my (Dutch) machine it doesn't seem to matter, but if you want to make absolutely sure the parsed-out date is regarded as being UTC (instead of its .Kind being set to 'Unspecified', use this:
[datetime]::SpecifyKind([datetime]::ParseExact('Mon Oct 18 10:26:10 EDT 2021', 'ddd MMM dd HH:mm:ss \E\D\T yyyy', [cultureinfo]'en-US').AddHours(4), 'UTC').ToLocaltime()

Convert Dates in Powershell but Format Not Recognized

I'm trying to use powershell to convert a string that contains RFC 2822 formatted dates into another format. Right now I'm getting an error saying my string was not recognized as a valid DateTime.
Write-Host ([Datetime]::ParseExact('Thu Oct 07 09:23:26 UTC 2021', '[R][1]', $null)).ToString("yyyyMMdd")
I'm not sure what I'm doing wrong.
The R standard datetime format string specifier does not describe the format you've supplied.
You're gonna need a custom format string:
[Datetime]::ParseExact('Thu Oct 07 09:23:26 UTC 2021', 'ddd MMM dd HH:mm:ss UTC yyyy', $null)

date gets increased by a day

I have a date in my mongodb database in following format
{
"_id" : ObjectId("5b8cd5e5cf36cb517c91910e"),
"date" : ISODate("2018-09-04T23:58:00.000Z")
}
When I show it by using moment on front end it gets increased by one day so 2018-09-04 becomes 2018-09-05
On front end I do this
moment(date).format('dddd MMM DD, YYYY')
I want same date extracted from the database that is 2018-09-04
Any help is appreciated.
Edit ->
When I console it on back end I see this
date: 2018-09-04T23:58:00.000Z
And on front end
date: "Thu Sep 06 2018 05:28:00 GMT+0530 (India Standard Time)"
Mongodb stores dates in ISO formats, it depends on where (in what timezones) your server and client are located. You can use the below.
The format for date you are getting is UTC format, when it will get converted to IST, it would show the difference. It means the following :-
If I get 2019-09-26T18:30:00.000Z as date in UTC. It is equivalent to Fri Sep 27 2019 00:00:00 GMT+0530 (India Standard Time). Please look at the difference 5hrs 30 mins. Incrementing/decrementing any thing on this and showing in IST would be wrong again as per the timezone is concerned.
If you do
new Date(date).toUTCString()
It will show you "Thu, 26 Sep 2019 18:30:00 GMT" In GMT which is exact.
It seems like a timezone issue.
If I run
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+00:00").format());
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+05:30").format());
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+00:00").format('dddd MMM DD, YYYY'));
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+05:30").format('dddd MMM DD, YYYY'));
I get
2018-09-04T23:58:00Z
2018-09-05T05:28:00+05:30
Tuesday Sep 04, 2018
Wednesday Sep 05, 2018
Which is off by one day

How to convert date in this format 'Dec-07-2013 09:32:05 CST' in GWT

In GWT Whats the correct format to convert date something like
Dec-07-2013 09:32:05 CST
Im using "mmm-dd-yyyy HH:mm:ss zzz" but getting UTC -5 timezone.
Is UTC -5 not equal to CST (during Dayligh tSaving Time)?
http://www.worldtimebuddy.com/?pl=1&lid=6,100&h=6

Unparseable Date error - Talend

I am trying to copy data from Excel to a SQL table.
I have dates generated in Excel file using RAND function. I am taking them as strings in an input and trying to convert them in date data type using tConvertType.
I have setted its datatype as 'string' in initial input and as 'date' in tConvertType's output and in tMSSqlOutput.
My job has work flow Excel input -> tConvertType -> tMap -> tMSSqlOutput.
While running the job I am getting an error which says :
java.text.ParseException: Unparseable date: "Tue Jul 17 00:00:00 EDT 1973"
I am not sure where the problem lies.
If anyone could help me with this it would be much appreciated.
Here's the screenshot of my job.
i am able to parse your given sample date please use below function in tMap for your date filed.
System.out.println(TalendDate.parseDateLocale("EEE MMM dd HH:mm:ss zzz yyyy", 'Tue Jul 17 00:00:00 EDT 1973', "EN"));
function is yourDateColumn!=null && !"".equalsIgnoreCase(yourDateColumn)? TalendDate.parseDateLocale("EEE MMM dd HH:mm:ss zzz yyyy", yourDateColumn, "EN") :null
#UmeshR: your code is working fine, but you have to handle the timezone thing as well. e.g. I am from India and when i converted the time from EDT talend converted it to my local timezone. see the screen-shot.