简体   繁体   中英

how to get the date in unix timestamp using javascript

I want to change my datetime : "2015-02-16 11:03:19.000000" into unix timestamp using javascript. I have tried the below code but its not wokring:-

var d = new Date("2015-02-16 11:03:19.000000");
document.write(d.getTime() + " milliseconds since 1970/01/01");

try using alert for you to see

var d = new Date("2015-02-16 11:03:19.000000");
alert(d.getTime() + " milliseconds since 1970/01/01")

your code is working

and look on this

convert date to timestamp in javascript?

How do you get a timestamp in JavaScript?

because your dateString style cannot be recognized by Date() Object. I have tried your code in Chrome, IE and Firefox. it works in Chrome, but not in Firefox and IE. so I recommend you try the method below.

Date() Object accepts four kinds of input.

  1. new Date();
  2. new Date(value);
  3. new Date(dateString);
  4. new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

if you choose dateString, you should enter string like "2015-02-16" or "2015-02-16T11:03:19" (date and time) can be passed and parsed. The UTC time zone is used to interpret arguments in ISO 8601 format that do not contain time zone information (note that ECMAScript ed 6 draft specifies that date time strings without a time zone are to be treated as local, not UTC).

and here is the relevant documention: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM