-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?

3
  • 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" Commented Oct 12, 2017 at 8: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" Commented Oct 12, 2017 at 9:09
  • 22:00:00 has a different equivalent UTC time in every timezone with a different offset. Commented Oct 12, 2017 at 9:41

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not having date along with that time string. I'm just having time = "22:00:00" how to get utc of that time
I updated my answer If you want todays date 22:00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.