formate date in javascript [duplicate] - javascript

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

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

calculate between expiration date and the current date then return the remaining days in javascript [duplicate]

This question already has answers here:
How to calculate date difference in JavaScript? [duplicate]
(24 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
Good day, I want to calculate the remaining days between the expiration date and the current date but it seems that the return is not what I expected.
function expiryDate(date_string) {
var date = date_string.split("/");
var year = parseInt(date[2]);
var day = parseInt(date[1]);
var month = parseInt(date[0]);
var expiration = new Date(month,day,year);
var d = new Date();
var curr_day = d.getDate();
var curr_month = d.getMonth()+1;
var curr_year = d.getFullYear();
var current_date = new Date(curr_month, curr_day, curr_year);
return (expiration - current_date) / (1000*60*60*24);
}
the code above will return the correct remaining days if the dates are the same for example.. the current date string was 05/01/2018 and the expiration is also the same and it will return 0, but when i move the expiration date to 1 day like 05/02/2018 the return is 28 days which is not correct.
How can I fix this problem?
As others have pointed out, months are zero indexed. Also, your method of zeroing the hours won't always work as expected because of daylight saving in some places. But rounding the result will remove DST effects.
There is no need for a library, your parse function and calculation can be be hugely simplified, you could easily remove another line from the following:
/* #param {string} date_string - date in m/d/y format
** #returns {number} days between today and expiry
*/
function expiryDays(date_string) {
var b = date_string.split(/\D/);
var expiry = new Date(b[2],--b[0],b[1]);
return Math.round((expiry - new Date().setHours(0,0,0,0)) / 8.64e7);
}
console.log(expiryDays('8/23/2018'));
// Or if you like obfuscated code
var expires = s=>Math.round((new Date(...(s.split(/\D/).reduce((a,v,i) => {a[(i+1)%3] = (i==0? v-1 : v);return a},[]))) - new Date().setHours(0,0,0,0))/8.64e7);
console.log(expires('8/23/2018'));
The Date object uses a zero-based month where January is 0, February is 1, etc. You seem to have tried in one place to compensate for that, but in the wrong way.
You need to fix this line like this:
var month = parseInt(date[0]) - 1;
And this line like this:
var curr_month = d.getMonth(); // No +1
Of course, using Moment.js, as suggested, is also a good idea, but I thought you might want to know how to get your own code working.
You can difference between two dates using momentjs's diff() function
Below is working code:
function expiryDate(date_string) {
var expiration = moment(date_string).format("YYYY-MM-DD");
var current_date = moment().format("YYYY-MM-DD");
var days = moment(expiration).diff(current_date, 'days');
return days;
}
alert("Days remaining = " + expiryDate("2018-05-05"));
console.log("Days remaining = " + expiryDate("2018-05-05"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>
initially working with times is a lot of work sometimes, and often you will invest a lot of time, maybe the moment.js library may be a solution for you depending on the need, specifically the isBetween
follow the link below.
moment.js
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days')

Convert json string to date in dd-mm-yy format using javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I want to bind data in a dd-mm-yy format for which I am using json to bind. Currently I get date as 2014-06-18T00:00:00
I want in dd-mm-yy format. Kindly let me know how to do that.
Below is my code for the same.
if (getJSONValue.LAUNCH_DATE != "" || getJSONValue.LAUNCH_DATE == null) {
$('#txtLaunchDate').val(getJSONValue.LAUNCH_DATE);
}
my getJSONValue.LAUNCH_DATE = 2014-06-18T00:00:00
see snippet
var newDate = new Date("2014-06-18T00:00:00");
var day = newDate.getDate();
var month = newDate.getUTCMonth() + 1;
var year = newDate.getFullYear();
console.log(day + "-" + ("0" + (month)) + "-" + year );
Using momentjs:
const date = '2014-06-18T00:00:00'
const format = 'DD-MM-YY'
console.log(moment(date).format(format))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>

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.

Day of tomorrow's show date in javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Add days to DateTime using JavaScript
if today's date of 5-12-2012, I want to display the date the next day 6-12-2012. How is that coded in javascript?
var date = "5-12-2012";
var datum = new Date(date);
datum.setDate(datum.getDate() + 1);
date = datum.getDate() + "-" + (datum.getMonth() + 1) + "-" + datum.getFullYear();

Categories

Resources