Compare date not working with toLocaleDateString() in javascript? [duplicate] - javascript

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>

Related

How to compare today date with another date in javascript? [duplicate]

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

How to format date for different language [duplicate]

This question already has answers here:
How to format date and display month and day based on user language
(2 answers)
Closed 3 years ago.
Now i have the english locale en-US and french locale fr-CA. How can i format a english date 05/31/2018 to french date 31/05/2018?
If you are trying to catch the actual datetime, you can do only setDate() with this function on your code:
function setDate(dt){
if(dt = "NaN"){
var date = new Date();
}else{
var date = new Date(dt);
}
var language = navigator.language;
var dateTime;
var day = date.getDate();
var month = date.getMonth()+1;
var year = date.getFullYear();
if(day < 10){
day = `0${day}`;
}
if(month < 10){
month = `0${month}`;
}
if(language == 'en-US'){
dateTime = `${day}/${year}/${month}`;
}else{
dateTime = `${day}/${month}/${year}`;
}
return dateTime;
}
But, if you have an especific datetime, you can add on console.log(setDate("05/31/2018"));.
This function will return you the date formated.

new Date().setFullYear(new Date().getFullYear()) returns a 13 digit number

When using var years = new Date().setFullYear(new Date().getFullYear()) I am getting a 13-digit number in return. (1521150199880)
The entire code block is as follows:
function changeYear(value) {
var years = new Date().setFullYear(new Date().getFullYear());
alert(years)
if (value < 0) {
year1 = year1 - 1;
document.getElementById("barText").innerHTML = years;
} else if(value > 0) {
years = years + 1;
document.getElementById("barText").innerHTML = years;
} else {
alert("Error!");
}
}
The program subtracts the year by one if the value input of the function is negative, and opposite for any other cases (Except when the value input is == 0, obviously)
Anyone see the problem? I have also tried exchanginf .getFullYear() with .getYear() without luck :/
setFullYear() returns The number of milliseconds between 1 January 1970 00:00:00 UTC and the updated date.
For your code to work
Replace this line
var years = new Date().setFullYear(new Date().getFullYear());
with this
var years = new Date(new Date().setFullYear(new Date().getFullYear())).getFullYear(); //as you want year from here
I don't know why you are doing this as new Date(new Date().setFullYear(new Date().getFullYear())).getFullYear() equals to new Date().getFullYear(). So you can also do this
var years = new Date().getFullYear();
The value for Date object is the number of milliseconds since midnight 1 Jan 1970 UTC.
Replace this line
var years = new Date().setFullYear(new Date().getFullYear());
with this
var years = new Date(new Date().setFullYear(new Date().getFullYear()));

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 two javascript dates [duplicate]

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.

Categories

Resources