How does Date.getHours() works - javascript

I can not clearly understand how does Date.getHours works. I live in montenegro and for me GMT is +2. I checked it in google and in browser console
new Date() // Date Tue Aug 02 2022 15:26:32 GMT+0200 (Central European Summer Time)
const date1 = new Date('December 31, 1975, 16:00:00 GMT+04:00');
const date2 = new Date('December 31, 1975, 16:00:00 GMT-04:00');
const date3 = new Date('December 31, 1975, 16:00:00 GMT+00:00');
console.log(date1.getUTCHours()); // 12
console.log(date2.getUTCHours()); // 20
console.log(date3.getUTCHours()); // 16
console.log('-------------------------')
console.log(date1.getHours()); // 13
console.log(date2.getHours()); // 21
console.log(date3.getHours()); // 17
UTC hours is I understand clearly plus/minus 4 hours and 0 hours. But why I get odd numbers when get non UTC hours?
I expected to get 14 in first logs because the time was in GMT+4, my timezone is GMT+2, so difference is 2 not 3
With same logic I expected to see 22, because difference is -6
And 18 in third log because difference is -2

Related

Getting value of specific time in next week

I am trying to get the value of next week ( + 7 days) at 09:00. I can get the Date using
new Date().setDate(new Date().getDate() + 7)
For example, it returns: 1619343639426
which translates to
new Date(1619343639426)
Sun Apr 25 2021 15:10:39 GMT+0530 (India Standard Time)
I want to get the value for Sun Apr 25 2021 09:00:00 GMT+0530 (India Standard Time)
how to do that ?
Try
new Date(new Date().setDate(new Date().getDate() + 7)).setHours(9, 0, 0, 0)

JS Date cannot get the last day of month with 31 days

This month (March) has 31 days.
I want to get the last day of the month and instead of get Wed Mar 31 2021 23:59:59 I get Fri Apr 30 2021 23:59:59 look:
let d = new Date()
d.setMonth( d.getMonth() + 1) // April
d.setDate(0) // should bring the 31 of March
d.setHours(23, 59, 59, 999)
console.log(d) // Fri Apr 30 2021 23:59:59 GMT+0300 (IDT)
Why does it happen on date with 31 days?
When tried on different months every month it worked as well, for example:
let d = new Date("2021-02-25") // notice that we point to February
d.setMonth( d.getMonth() + 1)
d.setDate(0)
d.setHours(23, 59, 59, 999)
console.log(d) // Sun Feb 28 2021 23:59:59 GMT+0200 (IST)
Notice that in the second example - which is working good, we get the last day of Feb and GMT+2 (IST) and not GMT+3 (IDT)
Also notice that if I declare it like that: let d = new Date('2021-03-25') it also works good (with specific date, instead of just new Date())
It happens because April only has 30 days.
let d = new Date()
d.setMonth( d.getMonth() + 1) // Actually April 31st -> May 1st.
Try this way:
d.setMonth(d.getMonth(), 0);
second argument 0 will result in the last day of the previous month
Got it!
I set +1 for the month while the current date is 31 and what will happen is that it will jump to 31 of April which doesn't exist and the default date will be 1 in May.
So prev date of 1 in May is 30 of April.
I should set the date to 1 before doing the increment of the month, look:
let d = new Date()
d.setDate(1) // this is the change - important!
d.setMonth( d.getMonth() + 1)
d.setDate(0)
d.setHours(23, 59, 59, 999)
console.log(d) // Wed Mar 31 2021 23:59:59
That way, it will start from 1 of March, inc to 1 of April, and go prev date to last day of March.
Even that it also works, weird:
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1, 0, 0, 0, 0);
var lastDay = new Date(y, m + 1, 0, 23, 59, 59, 999)
console.log(lastDay)

setMinutes not gettng expected result

I have the following js codes:
// set a date time say 2 Oct
var localTime = new Date(2016, 9, 2, 4, 0, 0);
// set time to 23:59:59
localTime.setHours(23, 59, 59, 0);
console.log(localTime); // // Sun Oct 02 2016 23:59:59 GMT+0800 (MYT), which is expected
// now minus 600 minutes, which should be 10 hours
localTime.setMinutes(-600);
console.log(localTime); // Sun Oct 02 2016 13:00:59 GMT+0800 (MYT)
When I minus 600 minutes from that time, I am expecting it to minus 10 hours which should be 13:59:59 but it's printing 13:00:59
What is that I am missing here?
Date.prototype.setMinutes does not add/remove minutes from the time you have. It sets the minutes value for your date. The argument you provide is:
minutesValue
An integer between 0 and 59, representing the minutes.

sethours updating time part but not the date part in date time field

I've a datetime field with date only as format. Also, I've added a script at onload so that whenever a record is accessed 12:00 should be added to that field. It works as expected and add 12 hours to the time part. But it do not update the date accordingly.
For example, I've Date Become Manager field and its value is 'Thu Apr 30 23:00:00 UTC-1200 1992'. And after adding 12 hours it updates the time part as 'Thu Apr 30 12:00:00 UTC-1200 1992' but do not add anything to its date. Following is my snippet for this to update.
function updateFields(field){
var dateField = Xrm.Page.getAttribute(field);
if(dateField.getValue()== null)
{
dateField.setValue(new Date());
}
dateField.setValue(dateField.getValue().setHours(12, 0, 0));
}
Please let me know if I am doing something wrong in it.
setHours only changes the time, it doesn't compute anything.
The most common way to perform this kind of computation is this:
var numberOfHours = 12; // how many hours you want to add. Can be *negative* too.
var millisecondsInAnHour = 60 * 60 * 1000; // this is constant
var offset = numberOfHours * millisecondsInAnHour;
var newFieldValue = dateField.getValue().getTime() + offset;
dateField.setValue(newFieldValue);
Basically, you grab the time of the value and add/subtract a number of milliseconds to it.
So to be clear, you want to add 12 hours to the current date value, (as opposed to just setting time element to 12:00)?
setHours just sets the time, it doesn't add 12 hours to the time. If you do it multiple times it will always be 12 hours, rather than 0 - 12 - 24.
If you combine setHours with getHours you should be able to achieve the desired behaviour.
var d1 = new Date();
console.log("Original Date: " + d1);
d1.setHours(12);
console.log("Set 12 Hours Once: " + d1);
d1.setHours(12);
console.log("Set 12 Hours Twice: " + d1);
var d2 = new Date();
console.log("Original Date 2: " + d2);
d2.setHours(d2.getHours() + 12);
console.log("Add 12 Hours Once: " + d2);
d2.setHours(d2.getHours() + 12);
console.log("Add 12 Hours Twice: " + d2);
Output:
Original Date: Tue Sep 22 2015 09:45:39 GMT+0100 (GMT Daylight Time)
Set 12 Hours Once: Tue Sep 22 2015 12:45:39 GMT+0100 (GMT Daylight Time)
Set 12 Hours Twice: Tue Sep 22 2015 12:45:39 GMT+0100 (GMT Daylight Time)
Original Date 2: Tue Sep 22 2015 09:45:39 GMT+0100 (GMT Daylight Time)
Add 12 Hours Once: Tue Sep 22 2015 21:45:39 GMT+0100 (GMT Daylight Time)
Add 12 Hours Twice: Wed Sep 23 2015 09:45:39 GMT+0100 (GMT Daylight Time)
I just updated my code and it works. Please have a look into following code snippet.
function updateFields(field){
var dateField = Xrm.Page.getAttribute(field);
if(dateField.getValue()== null)
{
dateField.setValue(new Date());
}
dateField.setValue(dateField.getValue().setHours(dateField.getValue().getHours() + 12));}

How to set Hours,minutes,seconds to Date which is in GMT

I have Date Object ,I wanted to clear HOUR,MINUTE and SECONDS from My Date.Please help me how to do it in Javascript. Am i doing wrong ?
var date = Date("Fri, 26 Sep 2014 18:30:00 GMT");
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Expected result is
Fri, 26 Sep 2014 00:00:00 GMT
How Do I achieve ?
According to MDN the setHours function actually takes additional optional parameters to set both minutes, seconds and milliseconds. Hence we may simply write
// dateString is for example "Fri, 26 Sep 2014 18:30:00 GMT"
function getFormattedDate(dateString) {
var date = new Date(dateString);
date.setHours(0, 0, 0); // Set hours, minutes and seconds
return date.toString();
}
You can use this:
// Like Fri, 26 Sep 2014 18:30:00 GMT
var today = new Date();
var myToday = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
Recreate the Date object with constructor using the actual date.
To parse the date into JavaScript simply use
var date = new Date("Fri, 26 Sep 2014 18:30:00 GMT”);
And then set Hours, Minutes and seconds to 0 with the following lines
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.toString() now returns your desired date

Categories