I am trying to add 3 years to a date that I have extracted from a cell on a Google sheet. My coded currently looks like this:
var courseDate = values[row][col];
var courseDay = courseDate.getMonth();
var courseMonth = courseDate.getMonth();
var courseYear = courseDate.getYear();
var renewalDate = new Date(courseYear + 3, courseMonth, courseDay);
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
var c = new Date(year + 3, month, day)
Related
This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 7 years ago.
I am trying to add months to a given date and want to output that date.
I am using this code, but it is not working.
var p = '2015-10-21';
var myDate = new Date(p);
var result1 = myDate.addMonths(3);
I want the result to be: 2016-01-21
The moment library is awesome for date related functions.
You can do operations like add, subtract, etc.
It has a syntax which is easily understandable.u can also avoid edge cases
If you included the moment library, your code could be rewritten like this
var p = '2015-10-21';
var myDate = moment(p, 'YYYY-MM-DD');
var result1 = myDate.add(3, 'months');
console.log (result1.format('YYYY-MM-DD'));
You can learn about or download moment from: http://momentjs.com/
You want to getMonth/setMonth
var p = '2015-10-21';
var myDate = new Date(p);
myDate.setMonth(myDate.getMonth() + 3);
Don't worry about overflow - Date object handles it
Note. This overwrites myDate, if you want the result in a different var
var p = '2015-10-21';
var myDate = new Date(p);
var result1 = new Date(myDate);
result1.setMonth(result1.getMonth() + 3);
as noted by #JohnHascall in the comments, this isn't fool proof around the end of month, for example adding three months to 30 November 2015 will result in 1st March 2016
You can try this -
<script>
var date = new Date();
var month = date.getMonth()+1;
var year = date.getFullYear();
var day = date.getDate();
function addMonth(addMonth){
var u_date,u_year,u_month;
u_date = month + addMonth;
if(u_date > 12){
var div = Math.floor(u_date / 12);
var rem = u_date % 12;
u_year = year + div;
u_month = month + rem;
document.getElementById("updated_date").innerHTML = day +" "+ u_month + " " + u_year;
}else{
document.getElementById("updated_date").innerHTML = day +" "+ (month+addMonth) + " " + year;
}
}
addMonth(13);
</script>
Here is the html-
<div id="updated_date"></div>
I want to look at the file created date instead of the current date and add 6 days so my between code will work properly.
var dateFrom = file.getDateCreated();
var dateTo = new Date();
dateTo.setDate(dateTo.getDate() + 6).toString();
var dateCheck = new Date();
dateCheck.setDate(dateCheck.getDate()).toString();
var from = Date.parse(dateFrom);
var to = Date.parse(dateTo);
var check = Date.parse(dateCheck );
if((check <= to && check >= from))
// alert("do something");
I have made a few attempts
var dateTo = (file.getDateCreated() + 7).toString();//failed attempt 1
var dateTo = new Date();
dateTo.setDate(dateTo.file.getDateCreated() + 6).toString();//failed attempt 2
var dateTo = new Date();
dateTo.setDate(dateTo.file.getDateCreated() + 6);//failed attempt 3
I am hoping someone can help me learn how to find success.
Regards,
Chris
This code determines whether today's date is at or past a target date. The target date is 6 days past the file creation date:
function olderThan() {
var file = DriveApp.getFileById('Your file ID');
var myFileDate = file.getDateCreated();
var creationDate = new Date(myFileDate);
var now = new Date();
var dateTo = new Date();
dateTo.setDate(creationDate.getDate() + 6);
if (now >= dateTo) {
Logger.log('its older than 6 days old');
};
};
I have to pretty much validate and check if the user is underage in ASP. So anyone under the age of 01/07/1998 is underage. I have 3 dropdown lists to select date, month and year. Im using JS. I try using concat and split but they dont work. Can anyone help with this?
function ValidateDate() {
var dDay = document.getElementById('DateList');
var dMonth = document.getElementById('MonthList');
var dYear = document.getElementById('YearList');
var day = dDay.selectedIndex;
var month = dMonth.selectedIndex;
var year = dYear.selectedIndex;
var firstValue = year + month + day;
var setyear = "1998";
var setmonth = "06";
var setdate = "01";
var secondValue = setyear + setmonth + setdate;
var firstDate = new Date();
firstDate.setFullYear(firstValue[0], (firstValue[1] - 1), firstValue[2]);
var secondDate = new Date();
secondDate.setFullYear(secondValue[0], (secondValue[1] - 1), secondValue[2]);
if (firstDate > secondDate) {
alert("Pass");
}
else {
alert("Fail");
}
}
<asp:CustomValidator ID="CustomValidatorDate" runat="server"
ErrorMessage=" You are underage" CssClass="error" Display="Dynamic" ClientValidationFunction="ValidateDate" ></asp:CustomValidator>
First of all your day, month and year variables are reading the index of the drop-downs and not their values.
For instance, you need to use:
var day = dDay.value,
month = dMonth.value,
year = dYear.value;
And then create date object as
var firstDate = new Date(year, month, day);
-Dipen
I am trying to find names of past days. I'm using this script to get names of past 7 days successfully, however, it doesn't work if I increase the past days, for example 7 days ago, 8 days ago or more.
var dayOfWeek = new Array();
dayOfWeek[0] = "Sun";
dayOfWeek[1] = "Mon";
dayOfWeek[2] = "Tue";
dayOfWeek[3] = "Wed";
dayOfWeek[4] = "Thu";
dayOfWeek[5] = "Fri";
dayOfWeek[6] = "Sat";
var myDate = new Date();
myDate.setDate(myDate.getDate());
var myDate1 = new Date();
myDate1.setDate(myDate1.getDate() - 1);
var myDate2 = new Date();
myDate2.setDate(myDate2.getDate() - 2);
var myDate3 = new Date();
myDate3.setDate(myDate3.getDate() - 3);
var myDate4 = new Date();
myDate4.setDate(myDate4.getDate() - 4);
var myDate5 = new Date();
myDate5.setDate(myDate5.getDate() - 5);
var myDate6 = new Date();
myDate6.setDate(myDate6.getDate() - 6);
var on1 = dayOfWeek[myDate.getDay()];
var on2 = dayOfWeek[myDate1.getDay()];
var on3 = dayOfWeek[myDate2.getDay()];
var on4 = dayOfWeek[myDate3.getDay()];
var on5 = dayOfWeek[myDate4.getDay()];
var on6 = dayOfWeek[myDate5.getDay()];
document.write(on5);
What is the correct way to do that ?
If I understand you correctly, you're trying to do something like (assuming today is Wednesday):
2 days ago was Monday
5 days ago was Friday
8 days ago was Tuesday
If this is correct, then this should do it:
function getDayOfWeekAgo(daysAgo) {
var today = new Date().getDay(),
days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
today = (today+daysAgo*6)%7; // adding 6 per day is the same as subtracting one
// due to the modulo, just without the complications
// that negative numbers would bring
return days[today];
}
Or, more efficiently:
window.getDayOfWeekAgo = (function() {
var today = new Date().getDay(),
days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
return function(daysAgo) {return days[(today+daysAgo*6)%7];}
})();
While working with date difference, when I am using below code somehow function is assuming that all the months have 31 days. For ex. if I am subtracting 01-March with 28-February the difference is coming as 4 days. Is there any simple way to twick this. Any help will be appreciated.
function myFunction()
{
var sysdt = "02/28/2013";
var year = sysdt.substring(6,10);
var mon = sysdt.substring(0,2);
var date = sysdt.substring(3,5);
var n = Date.UTC(year,mon,date);
var userdt = "03/01/2013"
var yr = userdt.substring(6,10);
var mn = userdt.substring(0,2);
var dd = userdt.substring(3,5);
var n1 = Date.UTC(yr,mn,dd);
var x = document.getElementById("demo");
x.innerHTML=(n1-n)/(1000*24*60*60);
}
This will give you the difference between two dates, in milliseconds
var diff = Math.abs(date1 - date2);
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.
U can subtract like this--
var d1 = new Date(); //"now"
var d2 = new Date("2011/02/01") // some date
var diff = Math.abs(d1-d2); // difference in milliseconds
var days = diff*24*60*60*1000;
You code is actually subtracting March 1 from April 1, as the months in JavaScript dates are 0-based.
var sysdt = "02/28/2013";
var date1 = new Date(sysdt);
var userdt = "03/01/2013"
var date2 = new Date(userdt);
var days = (date2-date1)/(1000*24*60*60);
or subtract 1 from month in your code
var sysdt = "02/28/2013";
var year = sysdt.substring(6,10);
var mon = sysdt.substring(0,2)-1; // months are from 0 to 11
var date = sysdt.substring(3,5);
var n = Date.UTC(year,mon,date);
var userdt = "03/01/2013"
var yr = userdt.substring(6,10);
var mn = userdt.substring(0,2)-1; // months are from 0 to 11
var dd = userdt.substring(3,5);
var n1 = Date.UTC(yr,mn,dd);
var days = (n1-n)/(1000*24*60*60);