I have a question regarding date formatting in Lua (Luajit). I need to get UTC string, for example, like I would do it in JavaScript:
var date = new Date()
console.log(date.toUTCString()) // "Fri, 06 Dec 2013 14:05:28 GMT"
Unfortunately in Lua I cannot find possibility to format date this way:
print(os.date()) -- Fri Dec 6 16:06:43 2013
From the Lua manual:
If format starts with '!', then the date is formatted in Coordinated Universal Time. [...]
If format is not "*t", then date returns the date as a string, formatted according to the same rules as the ANSI C function strftime.
Based on this and a little documentation referencing, it's quite simple to construct a format string that resembles JavaScript's toUTCString format.
> =os.date('!%a, %d %b %Y %H:%M:%S GMT')
Fri, 06 Dec 2013 14:27:34 GMT
Related
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)
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
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
I have an array:
my #array = ( "\"Passing\" on Wed 12 Jan 2015 09:19:14 AM PST",
"\"Passing\" on Wed 12 Jan 2015 09:19:25 AM PST",
"\"Test Activation\" on Tues 14 Jan 2015 12:05:14 PM PST",
"\"Run Phase\" on Tues 14 Jan 2015 12:06:14 PM PST",
"\"Test Activation\" on Tues 13 Jan 2015 11:43:12 PM PST")
I want to remove the duplicate string line BUT keep the one that is most recent. So I want it to look like:
my #array = ("\"Passing\" on Wed 12 Jan 2015 09:19:25 AM PST",
"\"Test Activation\" on Tues 14 Jan 2015 12:05:14 PM PST",
"\"Run Phase\" on Tues 14 Jan 2015 12:06:14 PM PST")
I can't think of an easy way to do this... I was thinking about using some regex to compare the strings ( /\".*\"/ ) and have it remove duplicates it finds, but I'm not sure how to deal with the date/time.
Any suggestions are most welcome!
There's several options to parse and compare the dates. Simplest is to use the built in Time::Piece. Use strptime for parsing and compare with $time->epoch.
Unfortunately for you, abbreviated time zone names are ambiguous. PST can mean US Pacific Standard Time or Philippine Standard Time. This may cause strptime's %Z format to choke, YMMV. From my strptime man page...
The %Z format specifier only accepts time zone abbreviations of the local time zone, or the value "GMT". This limitation is because of ambiguity due to of the over loading of time zone abbreviations. One such example is EST which is both Eastern Standard Time and Eastern Australia Summer Time.
You may need to pre-process the date formats and convert them to time zone offsets. You can use Time::Zone for this and its distinctly North American slant.
use Time::Zone;
use Time::Piece;
my $offset = sprintf "%+d", (tz_offset("PST") / 60 / 60);
my $time = Time::Piece->strptime(
"Wed 12 Jan 2015 09:19:14 AM $offset",
"%a %d %b %Y %I:%M:%S %p %z"
);
print $time->datetime, "\n";
print $time->epoch, "\n";
But try %Z first and see if it works.
Extracting the dates is also left as an exercise.
I'm working on a podcast (on-the-road events) that contains future dates and different time zones.
<pubDate>Wed, 14 Nov 2012 19:00:00 EST</pubDate>
<pubDate>Wed, 25 Jul 2012 19:00:00 CDT</pubDate>
<pubDate>Thu, 29 Mar 2012 19:00:00 MDT</pubDate>
When I parse it with PHP4 (note PHP4), using date() and strtotime(), it adjusts the information and outputs it in the current timezone of the server (including daylight saving time changes).
Code...
date("g:ia T", strtotime($item->pubDate));
Example...
<pubDate>Thu, 29 Mar 2012 19:00:00 MDT</pubDate> outputs 8:00pm CDT
The expected output is 7:00pm MDT
Again, I'm using PHP4, so I can't do DateTime()-type stuff.
Basically, I just want it to just return the characters, instead of interpreting the characters. Maybe I shouldn't use date() or strtotime() at all, but I'm not sure of another way to turn Wed into Wednesday, 19:00:00 into 7:00pm, etc.