Answer the question
In order to leave comments, you need to log in
How to convert regular date to unix timestamp?
Hello. Can you please tell me how to translate a date like "09/05/2017" is added using datapicker to unix timestamp?
There are two dates the patient needs to transfer these dates to unix and calculate among themselves.
PS All this is on jquery, something doesn't work for me ((
Answer the question
In order to leave comments, you need to log in
var date = "05.09.2017";
var timestamp = new Date(date.split(".").reverse().join(".")).getTime();
Here is a unique function to convert normal date to unix
function ToUnix(dateArg = {
str:false,
Y:"1970",
m:"01",
d:"01",
h:"01",
i:"00",
s:"00"
}){
Object.entries(dateArg).forEach(([k,v])=>{
if (Number.isInteger(v) && v < 10) {dateArg[k] = "0"+v}
if (!Number.isInteger(v) && v.length == 1) {dateArg[k] = "0"+v}
})
let dateStr = `${dateArg.Y}-${dateArg.m}-${dateArg.d}T${dateArg.h}:${dateArg.i}:${dateArg.s}`;
if (dateArg.str) dateStr = dateArg.str;
return +new Date(dateStr);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question