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 UNIX to readable date in javascript
(3 answers)
Closed 4 years ago.
JSFiddle: https://jsfiddle.net/u8w3v9fd/1/
I am trying to get the day, month and year from a date that is passed form the database in the format: DD/MM/YYYY. However I can't even seem to get the correct date to show.
Here is my code:
var time = "1522843537";
var regDateOriginal = new Date(time);
var regDate = new Date();
regDate.getMonth(regDateOriginal);
regDate.getHours(regDateOriginal);
regDate.getDate(regDateOriginal);
document.write("<p style='color: #fff'>" + regDate.getDate(regDateOriginal) + "</p>");
As you can see, this is returning:
21
Which is todays date. It should be 4
I have googled it and hacked around with various versions for the past 45 mins. I am a junior and would really appreciated a nicely commented piece of code so I can learn instead of just copying and pasting.
Thank you for your help.
From here
var time = 1522843537;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(time);
console.log(d.getDate());
Of course, you can still do all the other Date functions as needed.
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));
I have some JavaScript that pulls dates out of two fields on my web page:
var StartDate = document.getElementById('StartDate');
var EndDate = document.getElementById('EndDate');
When I get these two dates I have the following snippet of code that performs the date subtraction:
var day = 1000*60*60*24;
var d1 = new Date(StartDate.value);
var d2 = new Date(EndDate.value);
var difference = (Math.ceil((d2.getTime() - d1.getTime()) / day))
Now is when the problem comes in. Say that my two dates are as such:
StartDate = 2013-05-01
EndDate = 2013-06-30
Using the calculator we get:
1372564800000 - 1367380800000 = 5184000000
5184000000 / 86400000 = 60 days
However, next let's use the following dates:
StartDate = 2013-10-01
EndDate = 2013-11-30
Again, using the calculator we get:
1385787600000 - 1380600000000 = 5187600000
5187600000 / 86400000 = 60.04166666666667 days
I'm just not sure how this is possible, I am using two identical date ranges. Both start days start on the first of a month with 31 days, and both end days end on the last day of a month with 30 days. When I put these date ranges into a MS Excel workbook I get the correct number of days:
=(EndCell-StartCell)
And I again get the 60 days for both sets of date ranges.
This seems to only happen when I cross into November of 2013. It doesn't happen when I cross into November of 2014, and I cannot find any other times when this happens. I know 2013 is gone, but my application will deal heavily with 2013 dates. Does anybody know of a reason why/how this is happening? Does anybody know of a better way to do date subtraction with JavaScript that will not cause this issue?
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.