how to convert particular time into UTC timestamp [duplicate] - javascript

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
How to set Hours,minutes,seconds to Date which is in GMT
(3 answers)
Closed 5 years ago.
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?

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

Related

Convert 7:27:02 AM UTC time to local time-zone in Javascript. I'm getting 7:27:02 AM from the server [duplicate]

This question already has answers here:
Convert UTC date time to local date time
(38 answers)
Closed 3 years ago.
I'm trying to convert UTC time 7:27:02 AM to the local timezone. Converting just HH:MM:SS AM. Currently, I'm in GMT05:30.
Here, first, on lines 1 and 2, you get the current date and the current time zone offset, which is the difference in minutes (which you have to convert to milliseconds) between the UTC time and the local time. Then you'll get the timestamp from the date that you want, which is 7:27:02 AM (for PM you'll have to sum 12 hours), and subtract the offset. Then you just have to convert the timestamp to Date Object.
var date = new Date();
var offset=new Date().getTimezoneOffset()*60000;
var date2 = new Date(date.getFullYear(),date.getMonth(),date.getDay(),7,27,2).getTime()-offset
date2=new Date(date2)
console.log(date2)

Converting UTC format from server to local time and getting the time in HH:MM format [duplicate]

This question already has answers here:
How to show current time in JavaScript in the format HH:MM:SS?
(14 answers)
Closed 4 years ago.
I'm converting the utc from the server to local time using javascript and it works.But I need only the time rather than the date+time.Without using moment.
server time(UTC format) : "2018-02-03T10:00:00"
converted local time :2018-02-03T04:30:00.000Z
In the above local time I need only "04:30" alone .Is there any way to get it?
I tried the below one to convert from utc to local time
new Date(response.data.maxDate).toISOString();
console.log(new Date().toLocaleTimeString());
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
Date.prototype.toLocaleTimeString()
Returns a string with a locality sensitive representation of the time portion of this date based on system settings.
var time = datetime.toLocaleTimeString();
And if you want only hours:minutes then you can do this
date=new Date();
date.getHours() +':'+ date.getMinutes()
date=new Date();
console.log(date.getHours()+':'+date.getMinutes());

Create date from date time and time zone Javascript [duplicate]

This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 5 years ago.
Given inputs
1) Datetime : day month year hh and mins
2) Timezone
How to create a Javascript date object of a specific time zone or date in UTC ?
Also how to convert this date time to any other timezone.
Is there any package in angular that can do this.
So I do it like this:
var offset = -5.0; // my servers UTC offset
var clientDate = new Date();
var utc = clientDate.getTime() + (clientDate.getTimezoneOffset() * 60000);
var serverDate = new Date(utc + (3600000 * offset));
var c = $.datepicker.formatDate('mm/dd/yy', serverDate);
In the last line, I use the jQuery (commonly called BootStrap) datepicker to format the date for me.
The key here is offset which is the hours offset from UTC time.
In Angular you can use dateFilter
new Date(dateFilter(minDate, 'yyyy-MM-dd\'T\'00:00:00', timezone));

Add 30 days to the time now [duplicate]

This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 6 years ago.
I'm using Date.now() to get the current date.
I then need to add 30 days to this.
How can I do this?
Would I just work out how many seconds in 30 days, then add this on?
var d = new Date();
console.log(d);
d.setDate(d.getDate() + 30);
console.log(d);
Date.prototype.getDate returns current date day number.
Date.prototype.setDate set date number of given date to value passed to it.
If value exceeds normal date range of month date extra value will handle needed math and change month (or year if necessary) and shows correct result.
var date = new Date;
date.setDate(date.getDate() + 30);
console.log(new Date, date);

How do I turn a timestamp into a javascript date object? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert a Unix timestamp to time in Javascript
Turn a unix timestamp into 2008-07-17T09:24:17Z
How to do that?
Unix time stamp is in seconds since epoch right? You can convert it into milliseconds (by multiplying it by 1000) and passing it to the date constructor like this to convert it to a Date object.
new Date(unixtimestamp*1000)
Then you can use the Date APIs to get parts of the date.
unix timestamp has an accuration of second, so convert to milisecond and pass to Date constructor:
var d = new Date(timestamp * 1000)
As long as it's a valid date string, you should be able to get a Date object out of it using Date.parse().

Categories

Resources