Date minus 30 days error - javascript

I'm working on this function that compares today's date with an expiration date. The input: expireStamp, is a timestamp in milliseconds.
compDate = function(expireStamp) {
// expireStamp is a timestamp, convert it
var expireDate = new Date(expireStamp);
var notifyDate = new Date().setDate(expireDate.getDate() - 30);
var today = new Date(); // today
console.log("Today: " + today);
console.log("Notify: " + new Date(notifyDate));
console.log("Expire: " + expireDate);
if(today.getTime() <= notifyDate) {
// date is still good
return "good";
} else {
// date may be expired
if(today.getTime() > notifyDate && today.getTime() <= expireDate.getTime()) {
// date soon to expire
return "soon";
} else if(today.getTime() > expireDate.getTime()){
// date has expired
return "fail";
}
}
}
There are 2 dates to check today's date against, the expire date and the notify date which is 30 days before the expire date. The problem I'm having is with the notify date. If I set the expire date too far in the future, the notify date acts weird. Here's some example tests:
> var exp = new Date(1409362782000)
undefined
> exp
Fri Aug 29 2014 21:39:42 GMT-0400 (Eastern Daylight Time)
> var notify = new Date().setDate(exp.getDate() - 30);
undefined
> notify
1396183229815
> var test = new Date(notify);
undefined
> test
Sun Mar 30 2014 08:40:29 GMT-0400 (Eastern Daylight Time)
So, I set the expire date to August 29th (today is 4/4/2014), using a timestamp in milliseconds. That's quite a bit in the future. As you can see, exp is correct.
Notify date is supposed to be 30 days BEFORE exp but notify is March 30th which I am sure is way more than 30 days before August 29th. With dates closer to today it is fine.
I need the notify date to be 30 days before the expire date

> exp = new Date(1409362782000)
Sat Aug 30 2014 05:39:42 GMT+0400 (MSK)
> notify = new Date(exp.getTime() - (30 * 24 * 60 * 60 * 1000))
Thu Jul 31 2014 05:39:42 GMT+0400 (MSK)

Related

How to set time with AM PM in Javascript Date Object

Say that I have DateTime in this format Fri Feb 02 2018 00:00:00 GMT+0530 (IST)
And from the time picker plugin getting the time 1:10am or 2:30pm in this format.
I am not sure how to calculate and combine/add them both to produce this result:
Fri Feb 02 2018 01:10:00 GMT+0530 (IST) or Fri Feb 02 2018 14:30:00 GMT+0530 (IST)
I wish if there was something to do as simple as this:
new Date(dateString).setHours(1:10am)
Seems like you need to parse it on your own:
function parseDaytime(time) {
let [hours, minutes] = time.substr(0, time.length -2).split(":").map(Number);
if (time.includes("pm") && hours !== 12) hours += 12;
return 1000/*ms*/ * 60/*s*/ * (hours * 60 + minutes);
}
To add it to a date:
new Date(
+new Date("Fri Feb 02 2018 00:00:00 GMT+0530")
+parseDaytime("1:20pm")
);
Here is a simple function to do what your after.
It basically splits the time using a regex, and then calls setHours & setMins, adding 12 hours if pm is selected.
The example below takes the current datetime, and sets 1:10am & 2:40pm..
function setHours(dt, h) {
var s = /(\d+):(\d+)(.+)/.exec(h);
dt.setHours(s[3] === "pm" ?
12 + parseInt(s[1], 10) :
parseInt(s[1], 10));
dt.setMinutes(parseInt(s[2],10));
}
var d = new Date();
console.log(d);
setHours(d, "1:10am");
console.log(d);
setHours(d, "2:40pm");
console.log(d);
You can parse the time string into hours & minutes, adjust the hours according to am/pm & set it to the date object then:
var dateString = 'Fri Feb 02 2018 00:00:00 GMT+0530 (IST)';
var hoursString = '2:30pm';
var parts = hoursString.replace(/am|pm/, '').split(':')
var hours = parseInt(parts[0]) + (hoursString.indexOf('pm') !== -1 ? 12 : 0);
var minutes = parts[1];
var date = new Date(dateString);
date.setUTCHours(hours, minutes);
console.log(date); // in your local time
console.log(date.toUTCString()); // in UTC (i.e. without timezone offset)
(Note setHours / setUTCHours mutates date object but returns unix timestamp of the updated datetime.)

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

javascript date & time join

I have two date variable separately like following
startDate is a Date instance with the value Tue Jul 17 2012 00:00:00 GMT+0530 (IST)
startTime is a String with the value "11:30 AM"
Now what I need is join of both above date & time, as a Date.
startDateTime = Tue Jul 17 2012 11:30:00 GMT+0530 (IST)
I tried
new Date(startDate + " " + startDate) but outputting invalid date.
Also tried the way shown on this post. But still not working.
You can readily parse startTime if it's in a clearly-defined format, then use setHours and setMinutes: Live example | source
var startDateTime;
var parts = /^(\d+):(\d+) (AM|PM)$/.exec(startTime);
if (parts) {
hours = parseInt(parts[1], 10);
minutes = parseInt(parts[2], 10);
if (parts[3] === "PM" && hours !== 12) {
hours += 12;
}
else if (parts[3] === "AM" && hours === 12) {
hours = 0;
}
if (!isNaN(hours) && !isNaN(minutes)) {
startDateTime = new Date(startDate.getTime());
startDateTime.setHours(hours);
startDateTime.setMinutes(minutes);
}
}
...or something along those lines.
Note that key to this is the fact you've said startDate is a Date instance. The above assumes we're working within the timezone of the JavaScript environment, not across zones. If you were starting with a date string instead, and that string specified a timezone other than the JavaScript environment's timezone, which you were then converting into a Date via new Date("Tues Jul...."), then you'd have to be sure to adjust the resulting Date to use either the local time of the environment, or UTC; if you adjusted it to be UTC, you'd use setUTCHours and setUTCSeconds above instead of setHours and setSeconds. Again, this is only an issue if your starting point is a date string, and that string specifies a timezone different from the timezone in which the code above is running.
You can do This:
var theDate = new Date("Tue Jul 17 2012 00:00:00 GMT+0530 (IST)");
var theTime = "11:30 AM";
var hours = theTime .substr(0,2);
var minutes = theTime .substr(3,2);
var amOrPm = theTime .substr(6,2);
if (hours < 12 && "PM" == amOrPm) {
hours = +hours + 12;
}
theDate.setHours(hours);
theDate.setMinutes(minutes);
Try
new Date(startDate.toDateString() + " " + startTime)
This combines the date string from your Date object with the time string, and should give you a valid date. Note that this ignores the timezone you initially worked with, you might need to add " GMT+0530" again.
However, because your date string is already timezone-biased (Jul 16 2012, 20:30:00 UTC) it might be better to add them together, i.e. like new Date(+startDate + milliseconds):
var startDate = new Date("Tue Jul 17 2012 00:00:00 GMT+0530");
var startTime = "11:30 AM";
return new Date(+startDate + +new Date("1 1 1970 "+startTime))

Categories