I'm trying to get the expiry date and time of an AWS S3 object. I'm using Node.JS/JavaScript.
I can get the object data using the AWS SDK and s3.headObject(params) command and from the response I can get the Expiration string:
expiry-date="Wed, 20 Jan 2021 00:00:00 GMT", rule-id="ExpireCachedAssets"
How can I parse the string to get a JavaScript date object and check how long until it expires?
If the output always comes in that way, you can use regex to retrieve the date:
"([^"]+)GMT"'
that would match everything from " to the GTM", which retrieves date it as:
new Date('expiry-date="Wed, 20 Jan 2021 00:00:00 GMT", rule-id="ExpireCachedAssets"'.match('"([^"]+)GMT"')[0]);
// Wed Jan 20 2021 03:00:00 GMT+0300 (GMT+03:00)
P.S. You might want to use getTime() to get a UNIX timestamp.
P.S. S. Consider using fallback in case there is no match found.
Related
I am trying to display a date in javascript. I receive the date from backend like this: 2020-09-22T17:10:25Z (from and Instant object in Java).
When I try to call new Date("2020-09-22T17:10:25Z") I get: Tue Sep 22 2020 20:10:25 GMT+0300 (Eastern European Summer Time). The issue with this is that I am not in a GMT+0300 timezone but rather GMT+0200.
When I try to call new Date() on the other hand I get Thu Dec 08 2022 20:34:11 GMT+0200 (Eastern European Standard Time) which is my correct timezone.
My question is, why in the first case I get the GMT+0300 and in the second case I get GMT+0200? The Z in the string I am trying to parse stands for Zulu or zero hour offset, so why does the 2 different approaches use different timezones?
It looks like you are in GMT+2 in winter, but in summer (in September) you are in summer time which is GMT+3
javascript's date() function works off of the time set on your local computer. if it is giving GMT+3, then your computer is set to GMT+3. check your system clock's configuration.
windows: https://kb.wisc.edu/helpdesk/page.php?id=79027
mac: https://support.apple.com/en-ca/guide/mac-help/mchlp2996/mac
linux: https://www.makeuseof.com/how-to-set-date-and-time-linux/
I am using nodejs with the official mongodb driver.
I have documents with a date property stored as ISODate type.
The query is built beforehand and stored as a string ex. '{createdAt: {$gte: "FROM_DATE", $lte: "TO_DATE"}}' and passed onto server together with variables for fromDate and toDate.
There the query is parsed using json5 and "FROM_DATE" is replaced by new Date(fromDate) and same for the other.
The problem is new Date(fromDate) is always stored as string and I end up with the query below:
{createdAt: {
$gte: 'Tue Mar 10 2020 22:23:51 GMT+0100 (Central European Standard Time)',
$lte: 'Thu Apr 09 2020 22:23:51 GMT+0200 (Central European Summer Time)'
}}
Which as string it won't query ISODate format.
Is it just because I'm replacing string it gets returned as string by default or where is the issue? Thanks.
You can use the toISOString function while creating a date, as shown below
const date = new Date('01-JAN-2020');
console.log(date)
//Wed Jan 01 2020 00:00:00 GMT-0600 (Central Standard Time)
const newDate = new Date('01-JAN-2020').toISOString();
console.log(newDate)
//2020-01-01T06:00:00.000Z
I figured out the problem. The function .replace() always replaces string, with a string result.
I found the solution to avoid .replace() here if anyone has the same problem - https://stackoverflow.com/a/40436561/4693613.
I have a sql query for a column which takes the value of 'date' when I run SELECT * FROM ConferenceMetaData I get '2018-09-05'. The '2018-09-05' comes from the DB, but when I run the same SQL code in NodeJS the RecordSet returns "Tue Sep 04 2018 17:00:00 GMT-0700 (Pacific Daylight Time)".
Thus the question is: why am I not getting "2018-09-05" from running SELECT * FROM ConferenceMetaData in NodeJS?
nodeJS is interpreting the date returned from SQL Server as UTC date/time (2018-09-05 00:00:00) and it is converting that the current timezone, which assuming is PST/PDT would be 4th Sept # 5pm.
So, the answer is that you ARE getting 2018-09-05 00:00:00 UTC, only it is being expressed in PST/PDT timezone. Try looking at the UTC value of the date object in nodeJS.
I'm trying to convert a timestamp to a moment js object, like this:
let obj = moment.unix(1459382400);
It returns Wed Mar 30 2016 20:00:00 GMT-0400 (CDT) which is wrong, because the given timestamp corresponds to Thu, 31 Mar 2016 00:00:00 GMT.
Am I missing something? What am I doing wrong?
It is because of your timezone, do .utc() at the end.
Out of the box, momentjs is using your local timezone, so I guess there might be your problem.
You can use let obj = moment.unix(1459382400).utc(); instead, which should give you what you need.
Example here: http://jsfiddle.net/rLjQx/2544/
corresponding docs here: https://momentjs.com/docs/#/parsing/utc/
I'm sending the server time object with zero date, the sent date is:
Thu Jan 01 1970 01:02:01 GMT+0200
How can I convert it to GMT+0000? I need to tell the server about some task duration, so I want it to be just 01:02:01 as a duration. But the sent date is local and the server understands it as 03:02:01! How can I zero the GMT index?
Thanks
Getting the GMT time out of a JavaScript Date object is simple enough -
Date.prototype.toUTCString()
The toUTCString() method converts a date to a string, using the UTC time zone.
For example:
var test = new Date('Thu Jan 01 1970 01:02:01 GMT+0200').toUTCString();
console.log(test);
Note that this correctly outputs Wed, 31 Dec 1969 23:02:01 GMT, which although it not what you are looking for, is converting the provided Date to GMT.
To get what you want out of your input, a regular expression is useful. Caveats:
assumes duration will never be more than 23 hours, 59 minutes, and 59 seconds. If it is this will break.
var test = 'Thu Jan 01 1970 01:02:01 GMT+0200';
var durationMatcher = /\d\d:\d\d:\d\d/;
console.log(test.match(durationMatcher));
If you can, consider working in some values that works for you with one number - number of milliseconds for example.
function convertToGmt(pdate)
{
var newDate = new Date(pdate);
return (newDate.getUTCHours()<10?"0"+newDate.getUTCHours():newDate.getUTCHours())+":"+(newDate.getUTCMinutes()<10?"0"+newDate.getUTCMinutes():newDate.getUTCMinutes())+":"+(newDate.getUTCSeconds()<10?"0"+newDate.getUTCSeconds():newDate.getUTCSeconds());
}
Now use this function and call it by passing you date.
Notice that getUTCHours() returns correct hour in UTC.
Working Fiddle