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));
Related
This question already has answers here:
Display date/time in user's locale format and time offset
(17 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
i want get current time, when i convert new Date() into sting but when it converts reduces 5.30 hours based on my time zone.
How can i get same time which returns in new Date() method?
Try this
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
How ever format you want you may adjust
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)
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);
This question already has answers here:
Get the time difference between two datetimes
(22 answers)
Closed 5 years ago.
I am trying to get the day/hr/min difference from two JavaScript datetime stamps, the current time subtract a future event time which will then be displayed on the application front end.
two dates:
Future event date - 2017-10-01 18:00:00
Current date - now()
Current code:
var currentTime = new Date();
var eventStarts = results[0][i].eventstarts;
var difference = differenceInMilliseconds(eventStarts, currentTime);
var date = new Date(difference);
var days = date.getDay();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var formattedTime = days + ':' + hours + ':' + minutes.substr(-2);
This isn't correctly working and i cant figure out why, its returning a 2 day difference which obviously isn't correct, it should be returning upwards of 29 days.
I'm currently using the npm package date-fns and i don't mind trying out another package if that will help out
When you have a Date, it's based on the 1st january 1970. So when you are setting up new Date(difference). You are setting a date related to 1st january 1970 - so not what you are looking for.
I would recommand you to use the library moment.js, that allow easy date manipulation.
#manzurul example Here
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
console.log(d.days(), d.hours(), d.minutes(), d.seconds());
This question already has answers here:
How do I get the difference between two Dates in JavaScript?
(18 answers)
Closed 8 years ago.
I have a field at a grid containing date/time and I need to know the difference between that and the current date/time. What could be the best way of doing so?
The dates are stored like "2011-02-07 15:13:06".
This will give you the difference between two dates, in milliseconds
var diff = Math.abs(date1 - date2);
In your example, it'd be
var diff = Math.abs(new Date() - compareDate);
You need to make sure that compareDate is a valid Date object.
Something like this will probably work for you
var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));
i.e. turning "2011-02-07 15:13:06" into new Date('2011/02/07 15:13:06'), which is a format the Date constructor can comprehend.
You can just substract two date objects.
var d1 = new Date(); //"now"
var d2 = new Date("2011/02/01"); // some date
var diff = Math.abs(d1-d2); // difference in milliseconds
If you wish to get difference in wall clock time, for local timezone and with day-light saving awareness.
Date.prototype.diffDays = function (date: Date): number {
var utcThis = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
var utcOther = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
return (utcThis - utcOther) / 86400000;
};
Test
it('diffDays - Czech DST', function () {
// expect this to parse as local time
// with Czech calendar DST change happened 2012-03-25 02:00
var pre = new Date('2012/03/24 03:04:05');
var post = new Date('2012/03/27 03:04:05');
// regardless DST, you still wish to see 3 days
expect(pre.diffDays(post)).toEqual(-3);
});
Diff minutes or seconds is in same fashion.
Unless you are subtracting dates on same browser client and don't care about edge cases like day light saving time changes, you are probably better off using moment.js which offers powerful localized APIs. For example, this is what I have in my utils.js:
subtractDates: function(date1, date2) {
return moment.subtract(date1, date2).milliseconds();
},
millisecondsSince: function(dateSince) {
return moment().subtract(dateSince).milliseconds();
},
You can use getTime() method to convert the Date to the number of milliseconds since January 1, 1970. Then you can easy do any arithmetic operations with the dates. Of course you can convert the number back to the Date with setTime(). See here an example.