This question already has answers here:
Compare two dates with JavaScript
(43 answers)
Closed 2 years ago.
I am trying to compare old date with today's date but I am not getting output true. Please help me.
let today = new Date(); // 2021-02-13T13:28:28.501Z
if ("2020-11-04T08:01:25.698Z" < today){
console.log("True");
}
Variable today is a date object you need to compare it with date object only and then only you will get the desired results
if (new Date() === today ) console.log('true')```
You can compare two dates (Date objects) using getTime method
console.log(date1.getTime() > date2.getTime())
This example prints true if date1 is later than date2
In your case:
let today = new Date();
let otherDay = new Date("2020-11-04T08:01:25.698Z");
if (otherDay.getTime() < today.getTime()){
console.log("True");
}
You need to convert string to date first
if (new Date("2020-11-04T08:01:25.698Z") < today){
console.log("True");
}
let today = new Date(); // 2021-02-13T13:28:28.501Z
if (new Date("2020-11-04T08:01:25.698Z") < today)
{
console.log("True");
}
Convert string to Date object and compare.
you can create an isTodaySameDay function and call it.
check out the snippet code below:
const isTodaySameDay = (day) => {
const today = new Date();
return day.getFullYear() === today.getFullYear() &&
day.getMonth() === today.getMonth() &&
day.getDate() === today.getDate();
};
let yesterday = new Date();
yesterday.setDate(yesterday .getDate() - 1);
console.log(isTodaySameDay(yesterday)); //false
let today = new Date();
console.log(isTodaySameDay(today)); //true
Related
This question already has answers here:
Compare two dates with JavaScript
(44 answers)
Closed 5 years ago.
When i am trying to compare the two dates using date.toLocaleString() it is not giving the correct answer for some months date.
var date1 = new Date();
var date2 = new Date((new Date()).valueOf() + 1000*3600*24);
if(date1.toLocaleDateString() < date2.toLocaleDateString())
{
alert("Correct");
}
else
{
alert("Incorrect");
}
Can anyone have solution for this.
toLocaleDateString for todays date is 2018-2-21 where as for the month november, it is 2018-11-21. So in terms of string comparisons, november month will be considered to have a lesser value than todays date.
Use Timestamp to compare dates.
var date1 = new Date(),
date2 = new Date((new Date()).valueOf() + 1000*3600*24)
if(date1.getTime() < date2.getTime()) {
alert("Correct");
} else {
alert("Incorrect");
}
Try this:-
var date1 = new Date();
var date2 = new Date((new Date()).valueOf() + 1000*36000*24);
if(date1.getTime() < date2.getTime())
{
alert(""+date1+"Smaller then \n "+date2);
}
else
{
alert(""+date1+"\n "+date2);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
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
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.
I am trying to check if a date of format mm.dd.yyyy is greater than today and less than the date after 6 months from today.
Here is my code:
var isLinkExpiryDateWithinRange = function(value) {
var monthfield = value.split('.')[0];
var dayfield = value.split('.')[1];
var yearfield = value.split('.')[2];
var inputDate = new Date(yearfield, monthfield - 1, dayfield);
var today = new Date();
today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
alert(inputDate > today);//alert-> true
var endDate = today;
endDate.setMonth(endDate.getMonth() + 6);
alert(inputDate > today);//alert-> false
if(inputDate > today && inputDate < endDate) {
alert('1');
} else {
alert('2');/always alert it
}
}
If I execute isLinkExpiryDateWithinRange('12.08.2012') I wish it will show 1 as this is within the range, but it is displaying 2. Moreover the first alert is showing true and the second one false.
Can anyone please explain what is happening?
Change:
var endDate = today;
to:
var endDate = new Date(today);
See the posts here for how objects are referenced and changed. There are some really good examples that help explain the issue, notably:
Instead, the situation is that the item passed in is passed by value.
But the item that is passed by value is itself a reference.
JSFiddle example
function isLinkExpiryDateWithinRange( value ) {
// format: mm.dd.yyyy;
value = value.split(".");
var todayDate = new Date(),
endDate = new Date( todayDate.getFullYear(), todayDate.getMonth() + 6, todayDate.getDate() +1 );
date = new Date(value[2], value[0]-1, value[1]);
return todayDate < date && date < endDate;
}
isLinkExpiryDateWithinRange("12.24.2012"); // true
isLinkExpiryDateWithinRange("12.24.2020"); // false
Below function checks if date selected is within 5 days from today. Date format used is "DD-MM-YYYY", you can use any format by changing value.split('-')[1] order and split character.
function showMessage() {
var value = document.getElementById("invoiceDueDate").value;
var inputDate = new Date(value.split('-')[2], value.split('-')[1] - 1, value.split('-')[0]);
var endDate = new Date();
endDate.setDate(endDate.getDate() + 5);// adding 5 days from today
if(inputDate < endDate) {
alert("If the due date selected for the invoice is within 5 days, and express settlement fee will apply to this transaction.");
}
}
Is it possible to determine if a date is a Saturday or Sunday using JavaScript?
Do you have the code for this?
Sure it is! The Date class has a function called getDay() which returns a integer between 0 and 6 (0 being Sunday, 6 being Saturday). So, in order to see if today is during the weekend:
var today = new Date();
if(today.getDay() == 6 || today.getDay() == 0) alert('Weekend!');
In order to see if an arbitrary date is a weekend day, you can use the following:
var myDate = new Date();
myDate.setFullYear(2009);
myDate.setMonth(7);
myDate.setDate(25);
if(myDate.getDay() == 6 || myDate.getDay() == 0) alert('Weekend!');
You can simplify #Andrew Moore 's test even further:
if(!(myDate.getDay() % 6)) alert('Weekend!');
(Love that modulo function!)
The Date class offers the getDay() Method that retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc)
var date = new Date();
switch(date.getDay()){
case 0: alert("sunday!"); break;
case 6: alert("saturday!"); break;
default: alert("any other week day");
}
I think this is an elegant way to do this:
function showDay(d) {
return ["weekday", "weekend"][parseInt(d.getDay() / 6)];
}
console.log(showDay(new Date()));
Yes, it is possible, we can write a JavaScript code for that using JavaScript Date object.
Please use following JavaScript code.
var d = new Date()
document.write(d.getDay())
We can write a function to return the weekend in flag like below,
You can more customize the function to pass date. Or different return values for every day.
isItWeekEnd = function() {
var d = new Date();
console.log(d.getDay());
var dateValue = d.getDay();
// dateValue : 0 = Sunday
// dateValue : 6 = Saturday
if(dateValue == 0 || dateValue == 6)
return true;
else
return false;
}
var date = new Date();
var day = date.getDay();
if(day==0){
return false;
//alert('sunday');
}