Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date to timestamp?

I want to convert date to timestamp, my input is 26-02-2012. I used

new Date(myDate).getTime(); 

It says NaN.. Can any one tell how to convert this?

like image 785
selladurai Avatar asked Mar 26 '12 13:03

selladurai


People also ask

How do I convert a date to a timestamp in Excel?

Convert timestamp to date If you have a list of timestamp needed to convert to date, you can do as below steps: 1. In a blank cell next to your timestamp list and type this formula =(((A1/60)/60)/24)+DATE(1970,1,1), press Enter key, then drag the auto fill handle to a range you need.


2 Answers

Split the string into its parts and provide them directly to the Date constructor:

Update:

var myDate = "26-02-2012"; myDate = myDate.split("-"); var newDate = new Date( myDate[2], myDate[1] - 1, myDate[0]); console.log(newDate.getTime());
like image 177
The Alpha Avatar answered Oct 10 '22 01:10

The Alpha


Try this function, it uses the Date.parse() method and doesn't require any custom logic:

function toTimestamp(strDate){    var datum = Date.parse(strDate);    return datum/1000; } alert(toTimestamp('02/13/2009 23:31:30')); 
like image 43
Ketan Savaliya Avatar answered Oct 10 '22 00:10

Ketan Savaliya