This question already has answers here:
unexpected javascript date behavior
(3 answers)
Closed 7 years ago.
I have this JS:
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDay();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var milliseconds = d.getMilliseconds();
var timeStamp = year + '.' + month + '.' + day + '. ' + hours + ':' + minutes + ':' + seconds + ':' + milliseconds;
What it's outputting:
2015.10.5. 11:45:22:307
But what it should be outputting:
2015.11.5. 11:45:22:307
What is the problem with my script? Why the month is not correct?
Thank You for your help!
because getMonth method return the month from 0 to 11
http://www.w3schools.com/jsref/jsref_getmonth.asp
Related
This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 1 year ago.
I'm trying to add 3 days to a random date but instead it seems as though I'm adding a month.
var d = new Date(2021, 9, 14);
var currentTime = d.getTime();
var daysToAdd = 3;
var secondsInADay = 86400;
var d = new Date(currentTime + daysToAdd * secondsInADay);
var year = d.getFullYear();
var month = ("0" + (d.getMonth() + 1)).slice(-2);
var day = ("0" + d.getDate()).slice(-2);
console.log('result is:' + year + '-' + month + '-' + day);
Try this :
var d = new Date(2021, 9, 14) ;
var daysToAdd = 3;
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
console.log(date.addDays(daysToAdd));
You're multiplying by the number of seconds in a day, but you need to multiply by the number of milliseconds in a day, as shown below:
var d = new Date(2021, 9, 14);
var currentTime = d.getTime();
var daysToAdd = 3;
var milisecondsInADay = 86400000;
var d = new Date(currentTime + daysToAdd * milisecondsInADay);
var year = d.getFullYear();
var month = ("0" + (d.getMonth() + 1)).slice(-2);
var day = ("0" + d.getDate()).slice(-2);
console.log('result is:' + year + '-' + month + '-' + day);
This question already has answers here:
Javascript add leading zeroes to date
(30 answers)
How do I get Month and Date of JavaScript in 2 digit format?
(32 answers)
Javascript date - Leading 0 for days and months where applicable
(9 answers)
Closed 1 year ago.
I have one function that gives the date, the only problem is that my date is displayed like this on 5/31/2021, but I want to make it appear on 05/31/2021 here is my code
<span>{{ dtFormatter(course.updated_at) }}</span>
dtFormatter(d) {
var dateObj = new Date(d);
var day = dateObj.getUTCDate();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var year = dateObj.getUTCFullYear();
return day + "." + month + "." + year;
},
You can use padStart to add a leading Zero if needed:
day.padStart(2, '0') + '.' + month.padStart(2, '0') + '.' + year
day and month should be a string btw
You can use String#slice to add a leading 0 if needed:
('0' + day).slice(-2) + '.' + ('0' + month).slice(-2) + '.' + year;
Note: padStart doesn't work in IE.
I see you already calculated the day.
so you can do this.
`0${day}`.slice(-2)`
this will output 01~09 and 10~31.
As mentioned by #HassanImam, you could use a one-liner code:
new Date().toLocaleDateString('en-UK')
otherwise:
const date = new Date()
const m = String(date.getMonth() + 1).padStart(2, '0')
const d = String(date.getDate()).padStart(2, '0')
const y = String(date.getFullYear())
const formatedDate = [m, d, y].join('/')
console.log(formatedDate)
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I have a simple problem. i need to get only the date as string and remove its time.
How can i do this? I tried new Date() but its not working.
const value = '2018-04-09 00:00:00'
You can try this
const currentDate = new Date();
const formattedDate = ''
+ currentDate.getDate().toString().padStart(2, '0') + '-'
+ (currentDate.getMonth() + 1).toString().padStart(2, '0') + '-'
+ currentDate.getFullYear();
console.log(formattedDate)
// output
"09-05-2019"
function yyyymmdd(date) {
var mm = date.getMonth() + 1; // getMonth() is zero-based
var dd = date.getDate();
return [date.getFullYear(),
(mm>9 ? '' : '0') + mm,
(dd>9 ? '' : '0') + dd
].join('-');
};
let date=new Date()
console.log(yyyymmdd(date))
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>
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 6 years ago.
I want date with this format : '%Y-%m-%dT%H:%M:%S+0000'. I wrote a function but still asking myself if there is not better way to do this.
This is my function :
function formatDate() {
var d = new Date();
var year = d.getMonth() + 1;
var day = d.getDate();
var month = d.getMonth() + 1;
var hour = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();
var date = d.getFullYear() + "-" + (month < 10 ? '0' + month : month) + "-" +
(day < 10 ? '0' + day : day) +
"T" + (hour < 10 ? '0' + hour : hour) + ":" + (min < 10 ? '0' + min : min) + ":" + (sec < 10 ? '0' + sec : sec) + "+0000";
return date;
}
Any ideal on how to do this with less code ?
It can be done in one line. I made two lines to make it simpler. Combine line 2 and 3.
var d = new Date();
date = d.toISOString().toString();
var formattedDate = date.substring(0, date.lastIndexOf(".")) + "+0000";
console.log(formattedDate);
Use moment.js.
moment().format('YYYY-MM-DDTh:mm:ss+0000')
JSBIN
console.log(moment().format('YYYY-MM-DDTh:mm:ss+0000'))
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>
var d = new Date();
var dateString = d.getUTCFullYear() +"-"+ (d.getUTCMonth()+1) +"-"+ d.getUTCDate() + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds()+"+0000";
getUTCMonth returns 0 - 11, so want to add one before you convert to string.