[javascript] Convert normal date to unix timestamp

How can I convert normal date 2012.08.10 to unix timestamp in javascript?

Fiddle: http://jsfiddle.net/J2pWj/




I've seen many posts here that convert it in PHP, Ruby, etc... But I need to do this inside JS.

This question is related to javascript jquery date datetime unix-timestamp

The answer is


After comparing timestamp with the one from PHP, none of the above seems correct for my timezone. The code below gave me same result as PHP which is most important for the project I am doing.

_x000D_
_x000D_
function getTimeStamp(input) {_x000D_
    var parts = input.trim().split(' ');_x000D_
    var date = parts[0].split('-');_x000D_
 var time = (parts[1] ? parts[1] : '00:00:00').split(':');_x000D_
_x000D_
 // NOTE:: Month: 0 = January - 11 = December._x000D_
 var d = new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);_x000D_
 return d.getTime() / 1000;_x000D_
}_x000D_
_x000D_
// USAGE::_x000D_
var start = getTimeStamp('2017-08-10');_x000D_
var end = getTimeStamp('2017-08-10 23:59:59');_x000D_
_x000D_
console.log(start + ' - ' + end);
_x000D_
_x000D_
_x000D_

I am using this on NodeJS, and we have timezone 'Australia/Sydney'. So, I had to add this on .env file:

TZ = 'Australia/Sydney'

Above is equivalent to:

process.env.TZ = 'Australia/Sydney'

You could simply use the unary + operator

(+new Date('2012.08.10')/1000).toFixed(0);

http://xkr.us/articles/javascript/unary-add/ - look under Dates.


You can do it using Date.parse() Method.

Date.parse($("#yourCustomDate).val())

Date.parse("03.03.2016") output-> 1456959600000

Date.parse("2015-12-12") output-> 1449878400000


You can use Date.parse(), but the input formats that it accepts are implementation-dependent. However, if you can convert the date to ISO format (YYYY-MM-DD), most implementations should understand it.

See Why does Date.parse give incorrect results?.


You should check out the moment.js api, it is very easy to use and has lots of built in features.

I think for your problem, you could use something like this:

var unixTimestamp = moment('2012.08.10', 'YYYY.MM.DD').unix();

var datestr = '2012.08.10';
var timestamp = (new Date(datestr.split(".").join("-")).getTime())/1000;

_x000D_
_x000D_
var d = '2016-01-01T00:00:00.000Z';_x000D_
console.log(new Date(d).valueOf()); // returns the number of milliseconds since the epoch
_x000D_
_x000D_
_x000D_


convert timestamp to unix timestamp.

const date = 1513787412; const unixDate = new Date(date * 1000);// Dec 20 2020 (object)

to get the timeStamp after conversion const TimeStamp = new Date(date*1000).getTime(); //1513787412000


var date = new Date('2012.08.10');
var unixTimeStamp = Math.floor(date.getTime() / 1000);

In this case it's important to return only a whole number (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses Math.floor() and not Math.round()).


parseInt((new Date('2012.08.10').getTime() / 1000).toFixed(0))

It's important to add the toFixed(0) to remove any decimals when dividing by 1000 to convert from milliseconds to seconds.

The .getTime() function returns the timestamp in milliseconds, but true unix timestamps are always in seconds.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to date

How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Typescript Date Type? how to convert current date to YYYY-MM-DD format with angular 2 SQL Server date format yyyymmdd Date to milliseconds and back to date in Swift Check if date is a valid one change the date format in laravel view page Moment js get first and last day of current month How can I convert a date into an integer?

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?

Examples related to unix-timestamp

Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds Getting current unixtimestamp using Moment.js How to parse unix timestamp to time.Time Go / golang time.Now().UnixNano() convert to milliseconds? Get current NSDate in timestamp format MYSQL query between two timestamps How to get current time with jQuery Convert unix time to readable date in pandas dataframe How to get the unix timestamp in C#