admin管理员组

文章数量:1404759

I want to convert date into timestamp

I have two different format of date Wed May 31 2017 15:33:47 GMT+0530 (India Standard Time) and 2017-05-31T10:03:47.592Z. I want to convert it into timestamp like 1379426880000.

This is my code.

var noOfDays = 5;
var targetDate = new Date();

targetDate.setDate(targetDate.getDate() - noOfDays);
console.log("targetDate date is " + targetDate); //Wed May 31 2017 15:33:47 GMT+0530 (India Standard Time)
console.log(JSON.stringify(targetDate)); // "2017-05-31T10:03:47.592Z"

I want to convert date into timestamp

I have two different format of date Wed May 31 2017 15:33:47 GMT+0530 (India Standard Time) and 2017-05-31T10:03:47.592Z. I want to convert it into timestamp like 1379426880000.

This is my code.

var noOfDays = 5;
var targetDate = new Date();

targetDate.setDate(targetDate.getDate() - noOfDays);
console.log("targetDate date is " + targetDate); //Wed May 31 2017 15:33:47 GMT+0530 (India Standard Time)
console.log(JSON.stringify(targetDate)); // "2017-05-31T10:03:47.592Z"
Share Improve this question asked Jun 5, 2017 at 10:47 user7791120user7791120 5
  • you can use moment.js momentjs. – F.bernal Commented Jun 5, 2017 at 10:51
  • targetDate.valueOf() gives you the timestamp in milliseconds – Evan Sebastian Commented Jun 5, 2017 at 10:52
  • Possible duplicate of convert date to timestamp in javascript? – ponury-kostek Commented Jun 5, 2017 at 12:17
  • @EvanSebastian : targetDate.valueOf() is converting my date into timestamp but it's 1494135757264 of 13 Digit, while the timestamp which stores in my DB is 1496679262 of 10 digit. whats's that last 3 digit stand for – user7791120 Commented Jun 6, 2017 at 5:48
  • It's seconds vs milliseconds – Evan Sebastian Commented Jun 6, 2017 at 7:29
Add a ment  | 

1 Answer 1

Reset to default 4

Use this code to get the timestamp.

targetDate.getTime()

本文标签: javascriptHow to convert Date into TimestampStack Overflow