carbon_ghost
carbon_ghost

Reputation: 1124

S3 Node.js SDK, LastModified date format?

I'm using the S3 SDK for Node to make a request and list bucket contents (photos). I then need to sort these contents based on the LastModified date. This is the format of the date it's returning (which I'm not really sure what that format even is/why AWS is doing it like that:

Tue Jul 05 2016 11:00:52 GMT-0400 (EDT)

I tried using the Date.parse() method and using Moment.js to parse but to no avail.

In fact, Moment says it's an invalid date:

var day = moment("Tue Jul 05 2016 11:00:52 GMT-0400 (EDT)").isValid(); // false

Has anyone else experienced this before with the S3 Node SDK? And why is the date formatted like this?

Upvotes: 4

Views: 2712

Answers (2)

user238638
user238638

Reputation:

When I looked at this today, I noticed the format of an object's LastModified date in S3 is not the same as what was originally posted. This is the response I got back from S3: (notice the day and month are flipped)

LastModified: 'Mon, 06 Feb 2017 00:00:35 GMT'

So I had to update the format based on the answer from @idbehold

const lastModified = moment(result.LastModified, 'ddd, DD MMM YYYY HH:mm:ss ZZ');

Upvotes: 3

idbehold
idbehold

Reputation: 17168

This seems to parse your date correctly:

moment('Tue Jul 05 2016 11:00:52 GMT-0400 (EDT)', 'ddd MMM DD YYYY HH:mm:ss ZZ')

Upvotes: 1

Related Questions