Adding Day to Date [duplicate] - javascript

This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 5 years ago.
I have an example below of code that getting the date and adding certain days.
But the result I got from log is like these 1507824000000.
var endDate = new Date('10/03/2017');
var numOfDays = 10;
console.log(endDate.setDate(endDate.getDate() + numOfDays ));

If you want something to see your 10 days added, you can try the following :
var endDate = new Date('10/03/2017');
var numOfDays = 10;
endDate.setDate(endDate.getDate() + numOfDays);
var dd = endDate.getDate();
var mm = endDate.getMonth() + 1;
var y = endDate.getFullYear();
var yourNewDate = dd + '/'+ mm + '/'+ y;
console.log(yourNewDate)

Related

JavaScript - Trying to set tomorrow's date using the code below [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
var today = new Date();
var tomorrow = today.setDate(today.getDate() + 1)
console.log(tomorrow)
1596607917318
I am getting 13 digit number after using setDate(). How can I get the date in 2 digit format?
Date outputs in JS often need some manual processing to be exactly what you want. Try this:
// Create new Date instance
var today = new Date();
var tomorrow = today;
// Add a day
tomorrow.setDate(tomorrow.getDate() + 1)
console.log(formatDateToString(tomorrow));
function formatDateToString(date) {
var dd = (date.getDate() < 10 ? '0' : '')
+ date.getDate();
var MM = ((date.getMonth() + 1) < 10 ? '0' : '')
+ (date.getMonth() + 1);
return dd + "/" + MM;
}
The Date object has different methods that you can use to get certain parts of the timestamp.
// for day-month (i.e.: Oct 31 is 31-10
let formatted = `${tomorrow.getDate()}-${tomorrow.getMonth() + 1}`
See more: https://www.w3schools.com/js/js_date_formats.asp
setDate has changed the date of today.
Therefore output today and don't assign what's returned by setDate.
var today = new Date();
today.setDate(today.getDate() + 1);
console.log(today.toLocaleDateString());
Month is zero based so getMonth() + 1 returns this month, getDate() + 1 returns tomorrow.
var fecha = new Date();
var year = fecha.getFullYear();
var mes = fecha.getMonth() + 1;
var dia = fecha.getDate() + 1;
var hora = fecha.getHours();
var minutos = fecha.getMinutes();
var segundos = fecha.getSeconds();
var output = `Date: ${dia}/${mes}/${year}`+ '\n' + `Time: ${hora}:${minutos}:${segundos}`;
console.log(output)
Nice question, I recently had to do something similar in VB. Here is a simple javascript version, based on your code:
//this gets the date today
var today = new Date();
//we add one, to get the date tomorrow
var tomorrow = today.getDate() + 1
//if tomorrow is a single digit number, we just pad it with a zero
if (tomorrow < 10)
{
tomorrow = '0' + tomorrow
}
//write to the console
console.log(tomorrow)

Angularjs getcurrent time [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 3 years ago.
I am using the below code to get the current time in angularJS:
$scope.getDatetime = function() {
return (new Date()) + "abc.txt" ;
};
What is the correct code to the current time in YYYY-MM-DD-Hours-Minutes-Seconds?
Maybe you need this.
$scope.getDatetime = function() {
var date = new Date();
var day = date.getDate();
var month = ((date.getMonth() + 1) > 9) ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
return year+"/"+month+"/"+day+" "+hours+":"+minutes+":"+seconds;
};
var s = new Date().toISOString();
console.log(s);
s = s.slice(0,19);
s = s.replace('T','-');
s = s.replace(':','-');
s = s.replace(':','-');
console.log(s);

JavaScript struggling with date format [duplicate]

This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 5 years ago.
I am trying to retrieve a date but I am struggling with the month because January = 0 instead of 1. I need to retrieve items which occured after today -60 days.
var today = new Date();
if (today.getMonth() < 1) {
var numberOfDaysLastMonth = getDaysInMonth(12,today.getFullYear()-1); //number of days in december last year
var numberOfDaysThisMonth = getDaysInMonth(today.getMonth()+1,today.getFullYear()); //number of days this month
} else {
var numberOfDaysLastMonth = getDaysInMonth(today.getMonth(), today.getFullYear()); //number of days last month
var numberOfDaysThisMonth = getDaysInMonth(today.getMonth()+1, today.getFullYear()); //number of days this month
};
var startDate = new Date();
var myMonth;
var myYear;
if (today.getMonth() < 1) {
myMonth = today.getMonth() + 1;
myYear = today.getFullYear()-1;
} else {
myMonth = startDate.getMonth()-1;
myYear = today.getFullYear();
};
startDate = myYear+"-"+myMonth+"-"+startDate.getDate(); // returns last month
Is there a more simple (and working) way to do this?
If it's difficult to use dates as they are in Javascript, then modify them to work as they want them to work. An example:
Date.prototype.getMyMonth = function() {return this.getMonth() + 1;};
Now, you can change your code to use .getMyMonth instead of .getMonth.

javascript date adding days getting wrong days [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 5 years ago.
I am using this code on the page to show when the next few dates are, they are always the next few days... but it is not working now:
<script>var now = new Date();
var day = ("0" + (now.getDate()+3)).slice(-2);
var day2 = ("0" + (now.getDate()+4)).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (month)+"/"+(day)+"/"+now.getFullYear();
var today2 = (month)+"/"+(day2)+"/"+now.getFullYear();
document.write(today); document.write(' and/or ');document.write(today2);
</script>
But it is putting this out:
currently scheduled for: 05/32/2017 and/or 05/33/2017
how do I get the + 1 to have it go to the next month if it needs to?
Try this
<script>
var now = new Date();
var day = ("0" + (now.setDate(now.getDate()+3)).getDate()).slice(-2);
var day2 = ("0" + (now.setDate(now.getDate()+4)).getDate()).slice(-2);
var month = ("0" + (now.setMonth(now.getMonth() + 1+1)).getMonth()).slice(-2);
var today = (month)+"/"+(day)+"/"+now.getFullYear();
var today2 = (month)+"/"+(day2)+"/"+now.getFullYear();
document.write(today); document.write(' and/or ');document.write(today2);
</script>

Convert Military Hours to Normal Hours [duplicate]

This question already has answers here:
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 5 years ago.
I'd like my hours section to be set to 1–12 and not 0–23. Thank you.
Here's the JavaScript:
setInterval(function(){ time();}, 1000)
function time(){
var dates = new Date();
var newDates = dates.toDateString();
// var clock = dates.toLocaleTimeString();
var seconds = dates.getSeconds();
var minutes = dates.getMinutes();
var hours = dates.getHours();
var stringSeconds= String(seconds);
var stringMinutes= String(minutes);
var stringHours= String(hours);
newDate.textContent = newDates;
newDivSeconds.textContent = stringSeconds;
newDivMinutes.textContent = stringMinutes + ' :' ;
newDivHours.textContent = stringHours + ' :';
const usHours = (date.getHours() % 12) || 12;
Use the modulus operator
var usHours = date.getHours() % 12;

Categories