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

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

Related

Get month week number from a date (2022-02-28T10:00:53.393Z) -> 5 [duplicate]

This question already has answers here:
Moment.js how to get week of month? (google calendar style)
(14 answers)
Get week of the month
(17 answers)
Closed 12 months ago.
How to get week no from a date like moment("2022-02-28T10:00:53.393Z") should return 5.
I know about week() but that return week no from start of year like it will give 11 based on the date.
I need the week no. from month like week 1 of February or week 5 of February
Here is the solution for this
const getWeekOfDate = (date) => {
let startDate = moment(date).startOf('month').date();
let endDate = moment(date).date();
let weekCount = 1;
for (let i = startDate; i <= endDate; i++) {
if (
moment(date).date(i).format('dddd') ===
moment().startOf('week').format('dddd')
) {
weekCount++;
}
}
return weekCount;
};

convert the .getMonth to string month in javaScript(convert 0 to jan) [duplicate]

This question already has answers here:
Get month name from Date
(40 answers)
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 4 years ago.
i have the month and the array list. var month has the date month number. wanted to convert that number to alphabet.
function setDate(data){
var d = new Date(data.event_details.event_start_date);
var month = d.getMonth();
var m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
console.log(m);
}
function setDate(data){
var d = new Date(data.event_details.event_start_date);
var month = d.getMonth();
var m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
console.log(m[month]);
}
Use the index to get month
You can access the month by m[month]
Just access the array of month names using the result of getMonth() as the index.
function setDate(data){
const date = new Date(data.event_details.event_start_date);
const months = [
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
];
console.log(months[date.getMonth()]);
}
setDate({ event_details: { event_start_date: Date.now() } });
Alternatively, you can ditch the months array altogether and use toLocaleDateString.
function setDate(data) {
const date = new Date(data.event_details.event_start_date);
const month = date.toLocaleDateString("en-US", { month: 'short' });
console.log(month);
}
setDate({ event_details: { event_start_date: Date.now() } });
Because Date.getMonth() returns a number corresponding to the zero-based index of the month, you can simply use that number to access that index in your array (m).
const monthName = m[month]
indexOf is the function you need.
Here is the codepen link
function setDate(data){
var d = new Date(data.event_details.event_start_date);
var month = d.getMonth();
var m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var monthName=m[month];
console.log("month= " +monthName );
}

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.

javascript add month to date [duplicate]

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.

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

Categories

Resources