How to compare two javascript dates [duplicate] - javascript

This question already has answers here:
Compare two dates with JavaScript
(44 answers)
Closed 7 years ago.
I am trying to compare two java script dates using greater than operator but it is not working ,i am posting my code ,
select : function(date, jsEvent, allDay) {
$('#clickedDateHolder').val(date.format());
var check = date.format();
var date = new Date();
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
var currentDate = year +'-'+month+'-'+day;
// show modal dialog
alert('check :'+check +'currentDate :'+currentDate)
if(check < currentDate)
{
bootbox.alert("Past Dates")
}else if(check > currentDate){
input.push(date.format());
$('#selectedDate').val(input);
$('#event-modal').modal('show');
}
date.format(); is giving me the selected date and i am formatting the current date using
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
var currentDate = year +'-'+month+'-'+day;
but when i am using the greater than operator it is not working .The two date formats which are generated are
check :2015-10-27 currentDate :2015-9-29
How to solve this?? please help

You can get the time in milliseconds
check > new Date() // currentDate = new Date(), your string breaks it.

Related

new Date() produce a date that doesn't exist [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 8 months ago.
I'm using a code to produce continuous dates for a schedule, but one of the dates produced from this code does not exist (31/9/2022). Is there a way to prevent this?
var Sheet = ss.getSheetByName('list');
var Range = Sheet.getDataRange();
var Values = Range.getValues();
var Row = Values.length;
const year = 2022 //input year of first date
let month = 9 //input month of first date
let day = 5 //input day of first date
for (let i=0; i<Row-1; i++){
var date = new Date(year, month, day+i)
var fulldate = [date.getDate(), date.getMonth(), date.getFullYear()].join('/');
console.log(fulldate)
Values[1+i][0] = fulldate
Range.setValues(Values)
}
date.getMonth() function returns values from 0 to 11;
so if it returns 9 the month is October
learn more here: https://www.w3schools.com/jsref/jsref_getmonth.asp

Comparing dates in javascript doesn't always work [duplicate]

This question already has answers here:
Compare two dates with JavaScript
(44 answers)
Closed 6 years ago.
I am trying to compare two dates to see if one date is less than the other date, so I format both dates, then check if the expiry date is less than today and if it then show an alert message:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var today = dd+'/'+mm+'/'+yyyy;
var ExpiryDate = result.VATExpiryDate;
var day = ExpiryDate.getDate();
var month = ExpiryDate.getMonth()+1;
var year = ExpiryDate.getFullYear();
if(day<10){
day='0'+day
}
if(month<10){
month='0'+month
}
var ExpiryDate = day+'/'+month+'/'+year;
if(ExpiryDate < today && result.VATAuthNo.length>0)
{
alert("Please note Vat Authorisation Date for " + result.Name + " has expired - " + ExpiryDate);
}
But it seems it doesn't work for all dates. For example if the expiry date is 10/12/2015 it works and the alert message shows. But if the date is 21/06/2016 it doesn't work, even though that date is less than today.
You can compare dates operating directly with date object, not need to convert. javascript is a powerful language mate.
var today = new Date();
var expDate = new Date(2016, 10, 02)
if (today > expDate)
alert("expired");
Use the following method:
if( new Date(first).getTime() > new Date(second).getTime() ) {
// code;
}
var date1 = new Date('2017-02-15');
var date2 = new Date('2017-02-15');
if( date1 === date2 ){
console.log("bot are equal");
}
if( +date1 === +date2 ){
console.log("bot are equal");
}
You are comparing strings - to have correct results you shoud use format like YYYY-mm-dd not dd/mm/YYYY

how to compare a date with system date in javascript

I have to put a validation check that compares the date (ex:12-jun-2015) with the current system date in javascript. I am able to compare the date and year,but since the month is in the 'mon' format, so I am not able to compare with system date.
var date = new Date();
var currentMonth = date.getMonth();
var dateFields = (document.getElementById('startDate1').value.split('-'));
var screenMonth = dateFields[1];
Please suggest
This can be accomplished by using a month array containing all months code in 'mon' format.
var month = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
var date = new Date();
var currentMonth = date.getMonth();
var dateFields = (document.getElementById('startDate1').value.split('-'));
var screenMonth = dateFields[1];
if(sceenMonth.toLowerCase()==month[currentMonth])
// same month
else
// not same month

formate date in javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
compare todays date with calender date (for next seven days only else generate alert ) [closed]
(2 answers)
Closed 8 years ago.
var mydate = new Date();
var theyear = mydate.getFullYear();
var themonth = mydate.getMonth() + 1;
var thetoday = mydate.getDate();
txtbox.value="04-Jul-2012"
I have to convert todays date and txtbox date in "04/07/2012" format how i do it.
I have to compare todays date with txtbox date.
Thanks
Use date.js javascript library.
http://www.datejs.com/2007/11/27/getting-started-with-datejs/
var d1 = Date.parse('04-Jul-2012');
alert(d1.toString('dd/MM/yyyy'));
Date comparison
var today = Date.today();
var past = Date.today().add(-6).days();
var future = Date.today().add(6).days();
Date.compare(today, future); // -1
Date.compare(today, new Date().clearTime()); // 0
Date.compare(today, past) // 1

Javascript validation of date select boxes

I have created 3 select boxes containing days, months and year. What I really would like is to check after the user has selected a date, if the date is over a year from the current date a message is displayed or so.
Im a little stumped on what to do. Any gidance would be great.
Thanks
var ddlYear = document.getElementById('ddlYear');
var ddlMonth = document.getElementById('ddlMonth');
var ddlDay = document.getElementById('ddlDay');
var y = ddlYear[ddlYear.selectedIndex];
var m = ddlMonth[ddlMonth.selectedIndex];
var d = ddlDay[ddlDay.selectedIndex];
// past
var dt = new Date((y+1), (m-1), d);
var moreThanOnYearAgo = dt < new Date();
// future
var dt2 = new Date((y-1), (m-1), d);
var moreThanOnYearAhead = dt2 > new Date();
The y+1 is because if we're adding one year, and are still less than new Date() (today), then it's more than one year ago.
The m-1 is because months in the Date constructor are an enum, which means January is 0.
Don't reinvent the wheel one more time. Use a library that does validation.
There are 31556926000 milliseconds in a year. Just convert that date to a timestamp and subrtact the current date from it. If the result is greater than 31556926000 from it, is over a year away.
var userDate = new Date("11/29/2010");
var now = new Date();
var year_ms = 31556926000;
if ( userDate.getTime() - now.getTime() >= year_ms ) {
// A year away
} else {
// less than a year away
}

Categories

Resources