Add 10 more days to new Date Javascript [duplicate] - javascript

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 11 months ago.
So I'm getting the date to this format like this: 12.12.2023 with this function:
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 = dd + '.' + mm + '.' + yyyy;
And what I want to do is for example, add more 10 days to it. for example, if it is: 12.12.2023 to be 22.12.2023

let days = 10;
today.setDate(today.getDate() + days);

Related

Convert timestamp to yyyy-mm-dd hh:mm:ss format using javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How to format a Date in MM/dd/yyyy HH:mm:ss format in JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I need to convert the current timestamp (Eg: 1578293326452) to yyyy-mm-dd hh:mm:ss format
using javascript.
I obtained the current timestamp as follows:
var date = new Date();
var timestamp = date.getTime();
How can I change the format?
function getTime(){
var date = new Date();
var year = date.getFullYear();
var month = (date.getMonth() +1);
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return formateTime(year, month, day, hour, minute, second);
}
function formateTime(year, month, day, hour, minute, second){
return makeDoubleDigit(year) + "-" +
makeDoubleDigit(month) + "-" +
makeDoubleDigit(day) + " " +
makeDoubleDigit(hour) + ":" +
makeDoubleDigit(minute) + ":" +
makeDoubleDigit(second);
}
function makeDoubleDigit(x){
return (x < 10) ? "0" + x : x;
}
console.log(getTime())
Maybe this is what you need
d = Date.now();
d = new Date(d);
d = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+' '+(d.getHours() > 12 ? d.getHours() - 12 : d.getHours())+':'+d.getMinutes()+' '+(d.getHours() >= 12 ? "PM" : "AM");
console.log(d);

Get date prior to 30 days from today and prior to 12 months from today [duplicate]

This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 6 years ago.
I want to get date prior to 30 days from today and prior to 12 months from today. How Do I do that and format as mm/dd/yyyy
This is what I have done to set current date so far:
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;
today = mm+'/'+dd+'/'+yyyy;
You can use setDate and getDate to determine the date 30 days ago.
var date = new Date();
date.setDate(date.getDate() - 30);
Same thing can be done for months with getMonth and setMonth.

document.getElementById is null? ... what's wrong with my code..? [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 years ago.
Why am I getting "document.getelementbyId is null" ???
Any help is appreciate. thx. ... (and it says I need to type more text before I can submit... )
// get current date - in format
function thisDate(){
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth() + 1,
yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = mm + ',' + dd + ',' + yyyy;
dateDisplay = document.getElementById('dateDisplay')[0];
dateDisplay.innerHTML = today.toString();
console.log(today);
}
thisDate();
Remove [0] from your document.getElementById('dateDisplay')[0];
document.getElementById does not return an array, so there is no point in having an index pointer.

date now javascript convert format [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
how to convert timeInMs to this format dd/mm/yyyy
var timeInMs = Date.now();
alert(timeInMs);
//convert to dd/mm/yyy
is there any advantage of using jquery when dealing with time/date?
var date = new Date();
var year = date.getUTCFullYear();
var month = date.getUTCMonth();
var day = date.getUTCDate();
//month 2 digits
month = ("0" + (month + 1)).slice(-2);
//year 2 digits
year = year.toString().substr(2,2);
var formattedDate = day + '/' + month + "/" + year;
prints 25/10/13

javascript date format conversion [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I am using pikaday's datepicker. It gives me output "Fri Sep 20 2013". How can I convert this date into yyyy-mm-dd format and I also would want following date of this selected date and set that one to another element.
I tried this code
function formattedDate() {
var fromdate = new Date(document.getElementById('datepicker').value);
var dd = fromdate.getDate();
var mm = fromdate.getMonth()+1; //January is 0!
var yyyy = fromdate.getFullYear();
if(dd < 10)
{
dd = '0'+ dd;
}
if(mm < 10)
{
mm = '0' + mm;
}
var fromdate1 = dd+'/'+mm+'/'+yyyy;
fromdate.setDate(fromdate.getDate() + 2);
document.getElementById('datepicker').value = fromdate1;
var newdate = fromdate;
document.getElementById('datepicker1').value = newdate;
//alert(newdate1);
}
But it doesn't work.
var date = new Date(dateString);
var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var properlyFormatted = "" + year + month + day;
Or
var date = new Date(dateString);
var properlyFormatted = date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2);
Use momentjs — it's a library available for using in browser and node projects
In your case you should use this pattern:
moment().format("YYYY-mm-D");
And you can try it in console on momentjs's site:

Categories

Resources