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.
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.
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 7 years ago.
I am new to javascript.
I used following method to get date time.
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
var month = '0'+month;
}
if(day.toString().length == 1) {
var day = '0'+day;
}
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
if(second.toString().length == 1) {
var second = '0'+second;
}
var dateTime = day+'-'+month+'-'+year+' '+hour+':'+minute;
return dateTime;
}
I want output like :
day+'-'+month+'-'+year+' '+hour+':'+minute;
What is the best method for this
I think the best way to format date and time in Javascript is to use this lib. It's very simple to use.
var now = new Date();
dateFormat(now, "dd-mm-yyyy hh:MM:ss");
alert(now);
This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 9 years ago.
I want to add 1 Month or 6 Month to a given Date.
But if i add one Month, the year isnt incremented. And if i add 6 Month to June, i got the Month 00 returned BUT the year is incremented.
Could you please help me out?
function addToBis(monthToAdd){
var tmp = $("#terminbis").val().split('.');
var day = tmp[0];
var month = tmp[1];
var year = tmp[2];
var terminDate = new Date(parseInt(year),parseInt(month), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
day = "";
month = "";
year = "";
if(terminDate.getDate() < 10){
day = "0"+terminDate.getDate();
} else{
day = terminDate.getDate();
}
if(terminDate.getMonth() < 10){
month = "0"+terminDate.getMonth();
} else{
month = terminDate.getMonth();
}
year = terminDate.getFullYear();
$("#terminbis").val(day+"."+month+"."+year);
}
getMonth returns a number from 0 to 11 which means 0 for January , 1 for february ...etc
so modify like this
var terminDate = new Date(parseInt(year),parseInt(month - 1), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
and
month = terminDate.getMonth() + 1;
You should use the javascript Date object's native methods to update it. Check out this question's accepted answer for example, it is the correct approach to your problem.
Javascript function to add X months to a date
The function can be written much more concisely as:
function addToBis(monthToAdd){
function z(n) {return (n<10? '0':'') + n}
var tmp = $("#terminbis").val().split('.');
var d = new Date(tmp[2], --tmp[1], tmp[0]);
d.setMonth(d.getMonth() + monthToAdd);
$("#terminbis").val(z(d.getDate()) + '.' + z(d.getMonth() + 1)
+ '.' + d.getFullYear();
}
The value of terminbis and monthToAdd should be validated before use, as should the date generated from the value.