add prev or next date button in js - javascript

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.

Related

How to change the format of full date

Actually I made a function in which picks up the current date. Now i want to pickup the date after 6 months of my current date
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy ; //Here I am getting current date now i want to get the date exact after 6 months in the same format like this
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here I am getting date after 6 months but it is in different format like complete date, I want date after after 6 months like above format
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var date=today;
var x = 6; //or whatever offset
var CurrentDate = new Date();
//alert(CurrentDate);
CurrentDate.setMonth(CurrentDate.getMonth() + x);
var d = String(CurrentDate.getDate()).padStart(2, '0');
var m = String(CurrentDate.getMonth() + 1).padStart(2, '0');
var yy = CurrentDate.getFullYear();
CurrentDate = m + '/' + d + '/' + yy;
var months=CurrentDate;
Just pass new generated timestamp in new Date() and it will give you new date
Like var today_after_month = new Date(timestamp);
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
var today = mm + '/' + dd + '/' + yyyy ; //Here i m getting current date now i want to get the date exact after 6 months in the same format like this
console.log('today', today);
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here i m getting date after 6 months but it is different format like complete date i want date after after 6 months like above format
var today_after_month = new Date(month);
var dd = String(today_after_month.getDate()).padStart(2, '0');
var mm = String(today_after_month.getMonth() + 1).padStart(2, '0');
var yyyy = today_after_month.getFullYear();
var today = mm + '/' + dd + '/' + yyyy ;
console.log('month', today);

Use UTC timezone in Javascript date

This is the code I currently have but I am struggling to convert it to UTC
var today = Date.UTC(new Date());
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var H = today.getHours();
var i = today.getMinutes();
var s = today.getSeconds();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = yyyy + '-' + mm + '-' + dd + ' ' + H + ':' + i + ':' + s;
Any ideas on how I can get this working in the same timestamp format? Thanks!
Date objects are always stored in UTC - the .getXxx() functions you're calling implicitly convert that UTC time into your local timezone.
To extract the relevant fields in UTC time instead you should be using the .getUTCxxx() family of functions.
//
// returns the date and time in format 'YYYY-MM-DD hh:mm:ss'
//
// will take a `Date` object, or use the current system
// time if none is supplied
//
function UTC_DateTime(date) {
if (date === undefined) {
date = new Date();
}
function pad2(n) {
return (n < 10) ? ('0' + n) : n;
}
return date.getUTCFullYear() + '-' +
pad2(date.getUTCMonth() + 1) + '-' +
pad2(date.getUTCDay()) + ' ' +
pad2(date.getUTCHours()) + ':' +
pad2(date.getUTCMinutes()) + ':' +
pad2(date.getUTCSeconds());
}

Split Date.now() into parts in Javascript

I am trying to split the current date into parts and this is what I am doing
var today = new Date();
var currDay = today.getDay();
var currMonth = today.getMonth();
var currYear = today.getYear();
alert(currDay + "/" + currMonth + "/" + currYear)
but with this I get the following result
0/7/115 (output)
where it should be
09/07/2015
What can I do to get the required result?
Your "today.getDay()" should be "today.getDate()" since .getDay goes from 0-6 (days of the week)
var today = new Date();
var currDay = today.getDate();
var currMonth = today.getMonth();
var currYear = today.getFullYear();
alert(currDay + "/" + currMonth + "/" + currYear);
Try
var currYear = today.getFullYear();
Based on the answer from https://stackoverflow.com/a/3605248/2649340:
var today = new Date();
var output = ('0' + today.getDate()).slice(-2) + '/'
+ ('0' + (today.getMonth()+1)).slice(-2) + '/'
+ today.getFullYear();
getDay() gives you the day's number (0-6 for sunday to monday), month starts with 0 for january. This is how to do:
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
if(day<10) {
day='0'+day;
}
if(month<10) {
month='0'+month;
}
today = month+'/'+day+'/'+year;
alert(today);
you can use moment.js. It has a lot of helpful functions for time and date in JavaScript

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.

Add day(s) in a date in 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;
}

Categories

Resources