Add day(s) in a date in Javascript - javascript

I have textbox displaying date and have a button. the function in button is to add 7 days and display in textbox. my code:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}
The issue how to add a day to go to the next month.
Example the date in Textbox is 2014/08/25 when I click the button the date will be 2014/09/01

Just add 7 days to your date, date already handles change of month/year:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
addday.setDate(addday.getDate() + 7);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}

If you just do this
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
that means if date is 21.12.2014 the output will be 28.13.2014
function onNext() {
var startdate = document.getElementById('date').value;
var d2 = new Date(startdate);
d2.setMonth(d2.getMonth()+1);
d2.setDate(1); // you can set here whatever date you want
document.getElementById('date').value = d2.getFullYear() + '/' + d2.getMonth() + '/' + d2. getDate();
}

Use this function
function updateAb(s){//format dd/mm/yyyy chnage according to your need
var dmy = s.split("/");
var joindate = new Date(
parseInt(dmy[2], 10),
parseInt(dmy[1], 10) - 1,
parseInt(dmy[0], 10)
);
var data_days=7;
joindate.setDate(joindate.getDate() + data_days);
var cc=("0" + joindate.getDate()).slice(-2) + "/" +("0" + (joindate.getMonth() + 1)).slice(-2) + "/" +joindate.getFullYear();
document.getElementById("datepickerdisabled1").value=cc;
}

Related

add prev or next date button in js

window.onload = function () {
let today = new Date();
let D = String(today.getDate()).padStart(2, '0');
let M = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let YYYY = today.getFullYear();
today = YYYY + '-' + M + '-' + D;
let today2 = new Date();
today2 = M + "/" + D + "/" + YYYY;
var prevday = new Date();
prevday.setDate(prevday.getDate() - 1);
var nextday = new Date();
nextday.setDate(nextday.getDate() + 1);
addRow(
'<tr><td colspan="2"><h3>Events for Previous'
+ ' ' + today2 + ' '
+ 'Next</h3></td></tr>');
}
The code above to update the datepicker display date with previous and next date? I tried in PHP and JS and I did not have success on both.

Formatting date and time with zapier on javascript

I try to write this code to pass in a time and format it. It works on my IDE but when I pass it to zapier, it has an error.
This is my code
function dateConvert(dateobj,format){
var year = dateobj.getFullYear();
var month= ("0" + (dateobj.getMonth()+1)).slice(-2);
var date = ("0" + dateobj.getDate()).slice(-2);
var hours = ("0" + dateobj.getHours()).slice(-2);
var minutes = ("0" + dateobj.getMinutes()).slice(-2);
var seconds = ("0" + dateobj.getSeconds()).slice(-2);
var day = dateobj.getDay();
var months = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var dates = ["SUN","MON","TUE","WED","THU","FRI","SAT"];
var converted_date = "";
switch(format){
case "YYYY-MM-DD":
converted_date = year + "-" + month + "-" + date;
break;
case "YYYY-MMM-DD DDD":
converted_date = year + "-" + months[parseInt(month)-1] + "-" + date + " " + dates[parseInt(day)];
break;
}
return converted_date;
}
var date = input.VIP_2bParsed;
var format = "YYYY-MMM-DD DDD";
var converted_day = dateConvert(date,format);
output={converted_day: converted_day}
I have the following error: TypeError: dateobj.getFullYear is not a function
Full image of error here ERROR
Is VIP_2bParsed a variable you mapped in the Zap editor? If so, you'll want to access it with inputData.VIP_2bParsed instead of input.VIP_2bParsed.

loop through date and print formated date

for(i=0;i<30;i++){
}
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
I'm stuck, how can I print out the next 30 days?
like 3/3/2015, 4/3/2015, 5/3/2015 and so on..
var d = new Date();
var curr_month = d.getMonth();
curr_month = curr_month + 1;
var curr_date = d.getDate();
for( i = curr_date; i <= 31; i++ ){
var curr_year = d.getFullYear();
var date = curr_date + '/' + curr_month + '/' + curr_year;
curr_date++;
console.log(date);
}
Try this.It worked for me.
demo link
Try this:
var d = new Date();
var curr_day = d.getDate();
for(i=0;i<30;i++){
curr_day = curr_day+1;
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var newDate = new Date(curr_year,curr_month,curr_day)
var formatedDate = newDate.getDate() + '/' + newDate.getMonth() + '/' + newDate.getFullYear();
formatedDate;
}
You need to increment the day variable
You could put all that code below the loop inside the loop, then for each iteration add one day to the Date object. (add 24 * 60 * 60 * 1000)
var ONE_DAY = 24 * 60 * 60 * 1000;
var d = new Date();
for(i=0;i<30;i++) {
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
d = new Date(d.getMilliseconds() + ONE_DAY);
}
I would probably make slightly different style choices, but this is one way of doing it that pertains to the code you wrote.
Also, I don't have enough reputation to comment, but the other solution by pedrumj is very similar to mine, but keep in mind that it actually skips the first day (the new date should be assigned at the end of the loop)
Demo here
Use this
var d = new Date();
var curr_month = d.getMonth();
for(i=0;i<30;i++){
var curr_date = d.getDate();
var curr_month = curr_month + 1;
var curr_year = d.getFullYear();
var Printdate = curr_date + '/' + curr_month + '/' + curr_year;
console.log(Printdate);
}
You can reduce the date field by 1 day on each loop as follows:
var d = new Date(1425356823380);
for(i = 0; i < 30; i++){
var date = d.toLocaleDateString();
// code
d.setDate(d.getDate()+1);
}
Here is a fiddle
Your for loop doesn't loop through anything. You should set your fixed variables first, then loop through your variable you wish to change.
for(i=0;i<30;i++){
}
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
Like this:
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var curr_day = d.getDay();
var formatedDate = curr_month + '/' + curr_day + '/' + curr_year;
for(i=0;i<30;i++){
curr_day++;
formattedDate = curr_month + '/' + curr_day + '/' + curr_year;
console.log(formattedDate);
}
Mind, February only has 29 days at most.
See the Docs for more information. You may just want to use a library that can handle all the strange edge cases that happen with dates, such as MomentJS
for(var i=0;i<30;i++){
var start = new Date();
var next = new Date();
next.setDate(start.getDate() + i);
var date_str=next.getDay()+"/"+next.getMonth()+"/"+next.getFullYear()
console.log(next);
}
You can format start, next date as per your need.

Show current date

I am using the following to get the current date:-
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;
If I alert(newdate); it shows:-
3/06/2013
Is there any way I can display this as:-
03/06/2013
With plain Javascript, only manually
if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;
If you want to avoid using a library, and don't mind an extra line in your JavaScript:
var day = dateObj.getUTCDate(),
dd = parseInt(day, 10) < 10 ? '0' + day : day;
Using JQuery DateFormat:
$.format.date(dateObj.toString(), "dd/MM/yyyy");
var mydate=new Date();
alert(mydate.toString('dd/MM/yyyy'));
function padWithZeroes(number, width)
{
while (number.length < width)
number = '0' + number;
return number;
}
Now call day = padWithZeroes(day, 2) (and likewise for the month) before you use it.
You can split it and create your own format:
var splitTime = newDate.split("/");
var day = splitTime[0];
var month = splitTime[1];
var year = splitTime[2];
if (day < 10){
day = "0" + day;
}
var myDate = day + '/' + month + '/' + year;
Living demo:
http://jsfiddle.net/rtqpp/
Please use the following:
var dateObj = new Date();
var month = ('0' + (dateObj.getUTCMonth() + 1) ).slice( -2 );;
var day = ('0' + (dateObj.getUTCDate() + 1) ).slice( -2 );
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;

Ajax get Date in dd/mm/yyyy format

var d = new Date();
var today_date = d.getDate() + '/' + month_name[d.getMonth()] + '/' + d.getFullYear();
This is how I am getting a date. It works with a slight problem. For todays date 7th of June 2011 it returns 7/11/2011, what i want it to return is 07/11/2011?
Anyone know how?
Well, you could simply check the length of d.getDate()and if it's 1 then you add a zero at the beginning. But you would like to take a look at format() to format your dates?
Like so:
("0"+1).slice(-2); // returns 01
("0"+10).slice(-2); // returns 10
Complete example:
var d = new Date(2011,1,1); // 1-Feb-2011
var today_date =
("0" + d.getDate()).slice(-2) + "/" +
("0" + (d.getMonth() + 1)).slice(-2) + "/" +
d.getFullYear();
// 01/02/2011
Try this (http://blog.stevenlevithan.com/archives/date-time-format):
var d = new Date();
d.format("dd/mm/yyyy");
Try this, this is more understandable.:
var currentTime = new Date();
var day = currentTime.getDate();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
if (day < 10){
day = "0" + day;
}
if (month < 10){
month = "0" + month;
}
var today_date = day + "/" + month + "/" + year;
document.write(today_date.toString());
And result is :
07/05/2011

Categories

Resources