LEM
LEM

Reputation: 862

Unknown date format /Date(1427982649000-0400)/

I need to display on the screen some date values but I'm receiving them in a format that I don't know. Does anybody know what format is this and how to convert them?

For example, I'm getting this:

/Date(1427982649000-0400)/

In the database is stored as

2015-04-02 09:50:49.000

I really don't know where to start looking at.

Upvotes: 5

Views: 3901

Answers (3)

RobG
RobG

Reputation: 147503

Does anybody know what format is this and how to convert them?

It seems that "/Date(1427982649000-0400)/" is a time value in milliseconds followed by an offset as ±HHmm. To convert that to a Date, use the time value adjusted by the offset.

Assuming the offset uses the typical sign convention, then a positive offset needs to be subtracted and negative offset added to get the correct UTC value, then something like the following should suit:

var s = '/Date(1427982649000-0400)/';

// Get the number parts
var b = s.match(/\d+/g);

// Get the sign of the offset
var sign = /-/.test(s)? -1 : +1;

// Adjust the time value by the offset converted to milliseconds
// and use to create a Date
var ms = +b[0] + sign * (b[1].slice(0,2)*3.6e6 + b[1].slice(-2)*6e4);

console.log(new Date(ms).toISOString()); // 2015-04-02T17:50:49.000Z

In your example, "2015-04-02 09:50:49.000" does not have a timezone, so it represents a different moment in time for each timezone with a different offset. If that is the actual value stored in the database, then I guess the missing timezone is UTC-0800. It is much better to store the values using UTC and to include the offset, then the host timezone is irrelevant.

Things are complicated here because ECMAScript timezone offsets are the opposite sign to the normal convention, i.e. positive for west of Greenwich and negative for east. If that convention is applied, then "/Date(1427982649000-0400)/" converts to 2015-04-02T09:50:49.000Z, which may be what you're after.

If that is the case, just change the sign in the line:

var sign = /-/.test(s)? -1 : +1;

to

var sign = /-/.test(s)? +1 : -1;

Upvotes: 2

V.Deep
V.Deep

Reputation: 427

The actual time and date gets converted into the milliseconds, and it follows the Unix time January 1st, 1970. Because it is the date when the time for the Unix computer started.

But you can convert the milliseconds into the actual time by using some loops or conversions according to that time.

Upvotes: 4

Vivien Sonntag
Vivien Sonntag

Reputation: 4639

It's a unix timestamp in milliseconds, followed by a timezone (shift in hours differing from UTC).

So, it's UTC -4 hours, 1427982649 seconds after the 1st January of 1970.

Nice little tool for checking unix timestamps : http://www.unixtimestamp.com/index.php (don't forget to convert your milliseconds to seconds before posting them there)

/edit: To add some additional information - the "timezone shift" seems to be following RFC822 (and/or probably some other RFCs), that -0400 can be explained by the syntax "+/-HHMM" specified there, so to be exact it means -04 hours, 00 minutes.

Upvotes: 10

Related Questions