R
R
Roman2017-09-05 19:25:38
css
Roman, 2017-09-05 19:25:38

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

2 answer(s)
K
Kiryushka Tsisar, 2017-09-05
@carlcox

var date = "05.09.2017";
   var timestamp = new Date(date.split(".").reverse().join(".")).getTime();

V
Valera Dobroman, 2022-03-04
@Valera221

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);
}

Usage example:
asaToUnix({Y:1980,m:10,d:01,h:02,i:00,s:08}); //339210008
asaToUnix({str:"1981-01-02T10:10:05"}); // 347274605

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question