string to timestamp js code example
Example 1: convert date to timestamp javascript
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
alert(toTimestamp('02/13/2009 23:31:30'));
Example 2: timestamp to date javascript
let unix_timestamp = 1549312452
var date = new Date(unix_timestamp * 1000);
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
Example 3: js string to date
var myDate = new Date("2013/1/16");
var str = "2013/1/16";
var strToDate = new Date(str);
Example 4: convert datetime to timestamp javascript
function toTimestamp(strDate){ var datum = Date.parse(strDate); return datum/1000;}
Example 5: convert date to timestamp javascript
const toTimestamp=(strDate)=>{
var datum = Date.parse(strDate);
return datum/1000;
}
console.log(toTimestamp('02/13/2009 23:31:30'));
Example 6: how to validate from and to date using date.parse in javascript
var stringval = '01/03/2012';
var testdate;
try {
testdate = $.datepicker.parseDate('mm/dd/yy', stringval);
} catch (e)
{
alert(stringval + ' is not valid. Format must be MM/DD/YYYY ' +
'and the date value must be valid for the calendar.';
}