Add one Day at the Date change also the hour - javascript

I have issue with a date.
I have two date (start and end) and I need to build an array of all date between these two.
My script is something like:
while(currentData < nodeLastDate){
currentData.setDate(currentData.getDate() + 1);
console.log(currentData)
}
But at Sat Mar 30 2019 there is an error and the data change also the time.
if you run this simple script you can see it.
let test = new Date(2019, 2, 30, 2)
console.log(test)
test = test.setDate(test.getDate() + 1)
console.log(new Date(test))
this is the result:
Sat Mar 30 2019 02:00:00 GMT+0100 (Ora standard dell’Europa centrale)
index.js?c69d:385 Sun Mar 31 2019 03:00:00 GMT+0200 (Ora legale dell’Europa central)
Is this normal?

Date.getDate() gets the day of the month, so you lose any other information. If you want to add a day to a date, simply use Date.getTime() and add the number of milliseconds in a day:
let test = new Date(2019, 2, 30, 2)
console.log(test)
test = test.setDate(test.getTime() + (1440 * 60000))
console.log(new Date(test))
Date.getTime returns the number of milliseconds since an arbitrary date known as the epoch date, so adding the number of milliseconds in a day will add exactly one day to your date. (1440 * 60000 is the number of milliseconds in a day because there are 1440 minutes in a day and 60000 milliseconds in a minute)

Related

Time until midnight CST?

I have this code to calculate, based on the user's computer's time, the milliseconds until Midnight CST.
For the timezone I am in, now.getTimezoneOffset() returns 420, making tmzOfst 60.
function millisToMidnight() {
var now = new Date();
var tmzOfst = (now.getTimezoneOffset())-360; //-360 minutes = CST
now.setHours(-(tmzOfst/60));// Adjust 'now' to CST time
var then = new Date(now); //make a var same as now
then.setHours(24, 0, 0, 0); //set to midnight
return (then - now); //calculate difference
}
However, when I run this (console.log's everywhere), I get this:
Now = Tue Mar 07 2017 21:51:05 GMT-0700 (Mountain Standard Time)
tmzOfst = 120
Then = Mon Mar 06 2017 22:51:05 GMT-0700 (Mountain Standard Time)
Which, as you can see, correctly changes the time to CST, however, it ends up changing the date one day as well. Is there a easier way to do this? Why does it change the day?
if you want to ADJUST the hours, you need to adjust, not SET
now.setHours(now.getHours()-(tmzOfst/60));
This should give you the milliseconds for the coming midnight.
function millisToMidnight() {
var date = new Date();
date.setDate(date.getDate() + 1);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
return date.getTime() - (new Date()).getTime();
}

Incorrect Javascript Day When Added?

I have a javascript function that takes in a number X and a date, and returns a new Date that is X number of days away:
function addDays(theDate, numDaysToAdd) {
var newDate = new Date();
return new Date(newDate.setDate(theDate.getDate() + numDaysToAdd));
}
I pass it a day that is Sat Jul 02 2016 16:03:06 GMT-0700 (Pacific Daylight Time) and a number 7, but the result I got was Thu Jun 09 2016 16:05:32 GMT-0700 (Pacific Daylight Time). Why is it giving me the correct date but wrong month?
The problem is that newDate is always created from the current date (new Date()). In other words, if this function is executed in June it will produce a date in June, then try to set a the day of the month as a offset from the input date.
You need to construct newDate as a copy of theDate:
function addDays(theDate, numDaysToAdd) {
var newDate = new Date(theDate);
newDate.setDate(theDate.getDate() + numDaysToAdd);
return newDate;
}
var d = new Date('Sat Jul 02 2016 16:03:06 GMT-0700 (Pacific Daylight Time)');
console.log(addDays(d, 7).toString());
You can add number of milliseconds to given date and it will generate correct date.
getTime() returns milliseconds from epoch.
offset = numDaysToAdd * 24 * 60 * 60 * 1000;
24: Hours in a day
60: Minutes in an hour
60: seconds in a minute
1000: milliseconds in a second
Date constructor takes milliseconds from epoch
function addDays(theDate, numDaysToAdd) {
var start = theDate.getTime();
var offset = numDaysToAdd * 24 * 60 * 60 * 1000;
return new Date(start + offset);
}
var today = new Date();
console.log(today, addDays(today, 10));

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

Date Object and UTC Method

Why is this returning the 31st instead of the 1st? As I understand it, the UTC method requires 3 parameters ( full year, month, day ), and the day parameter must be an integer between 1 - 31. Because getDate() returns with an integer between 0 and 31, I also suspected 0 would be a possibility.
firstDay = new Date(Date.UTC( 2011 , 7 , 1 )).getDate();
// returns 31 (last day of this month)
Let me clarify and say, this is not a special case. With the day parameter as 2, 3, or 4, this will return 1, 2, 3, and so on.
Your timezone offset is negative so like -4. So 7/1/2011 at 12:00AM minus 4 hours is 6/31/2011 8:00PM. Date.UTC takes additional parameters that you can use to pass hours, minutes, seconds, and milliseconds.
But really, if you don't want the timezone adjustment use new Date(year, month, day)
firstDay = new Date(2011 , 7 , 1).getDate(); // returns 1 (first day of this month)
I am at (GMT-0700) Pacific Time. Here are the results when I conduct the following:
new Date( 2011, 7, 1 );
// -> Mon Aug 01 2011 00:00:00 GMT-0700 (Pacific Daylight Time)
new Date( Date.UTC( 2011, 7, 1 ) );
// -> Sun Jul 31 2011 17:00:00 GMT-0700 (Pacific Daylight Time)
Notice that pulling the UTC time gives me the date/time in my current location, 7 hours prior to the date specified because I'm 7 hours behind Greenwich Mean Time.
This is what you're looking for:
new Date(Date.UTC(2011, 7, 1) + ((new Date).getTimezoneOffset() / 60) * 3600000).getDate();
Explanation:
(new Date).getTimeZoneOffset(); // will retrieve the timezone offset, (420 in my case PST)
offset / 60; // we divide by 60 so we can get the number of hours between UTC and me
(offset / 60) * 360000 // is 60 (seconds) * 60 (minutes) * 1000 (ms)
+ Date.UTC(2011, 7, 1) // will give us the correct Date always

Categories