-3

I'm having a time in this format like

var time = "22:00:00"

I need to convert this into UTC time format like 1567890764 I'm using this one to convert time into UTC.

Math.floor(new Date().getTime() / 1000) //it return current UTC timestamp

how to pass the "time" variable and get UTC timestamp for that particular time?

Saravanan CM
  • 39
  • 1
  • 5
  • 17
  • 1
    There is no such thing as a _"UTC time format"_. That timestamp you posted is a full _date_. There's a year / month / day etc in there as well. `1567890764` is the timestamp for _"GMT: Saturday, September 7, 2019 9:12:44 PM"_ – Cerbrus Oct 12 '17 at 08:59
  • Yeah i need the timestamp of today date with time "22:00:00". How to achieve that only by having time = "22:00:00" – Saravanan CM Oct 12 '17 at 09:09
  • 22:00:00 has a different equivalent UTC time in every timezone with a different offset. – RobG Oct 12 '17 at 09:41

1 Answers1

1

You can just create UTC Timestamps with the full date. You can use the .getTime() function of the Date object. You will get the milliseconds since 1970/01/01 and then divide it with 1000 to get the seconds.

let datestring = "2017-10-21 13:22:01";
let date = new Date(datestring);
console.log(date.getTime() / 1000); // will return 1508584921

You can also take a look at Moment.js which is great for handling Date and Time in Javascript.

EDIT: If you want todays date 22:00:00 just init new Date and set the time like this:

let date = new Date();
date.setHours(22);
date.setMinutes(0);
date.setSeconds(0);
Stefan F.
  • 138
  • 1
  • 8