Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Darrel Lyons
2,991 PointsDate using Javascript
I need javascript to add so many days to todays date and output the resulting date.
for example, todays date is 16/05/2014. If I added 12 days to that, it would make it 28/05/2014.
I'm trying to help out my brother with a project and i would like to sound like i know what i'm going on about. I only joined this site last night and i love it but can anyone help me out for today please? The code has to be in Javascript as the page will be hosted on a computer so no PHP please, Thanks.
6 Answers
James Barnett
39,199 PointsThat's really easy to do with moment.js
var date = "16/05/2014";
var daystoAdd = 12;
//parse date, add specified number of days, format date
var newDate = moment(date, "DD/MM/YYYY").add('days', daystoAdd).format("DD/MM/YYYY");
I made you a demo: http://jsbin.com/nazuq/2/watch?html,console
Dino Paškvan
Courses Plus Student 44,108 PointsIs this what you're looking for?
// Extend the Date object with a new method
Date.prototype.addDays = function(days) {
var tempDate = new Date(this.valueOf()); // get the Date from the object the method was called on
tempDate.setDate(tempDate.getDate() + days); // set a new date by adding the number of days passed to the method
return tempDate; // return the new date
};
// create a new date
var myDate = new Date();
console.log(myDate);
// create a new date in future based on the first date
var dateInTwelveDays = myDate.addDays(12);
console.log(dateInTwelveDays);
Dino Paškvan
Courses Plus Student 44,108 PointsOkay, this CodePen contains what you need to add days to a date, format it and then display it on the page. As I don't know what JavaScript libraries your page might be using (if any), I've written vanilla JavaScript code. Besides, if you only need adding days and formatting, any library would be overkill anyway.
I hope this helps you.
Darrel Lyons
2,991 PointsThank you but how do i get that showing on the page?
Dino Paškvan
Courses Plus Student 44,108 PointsYou'll have to explain the project in a bit more detail, as I don't know where the date is supposed to be displayed, or in which format. Is it the only thing displayed on the page?
Darrel Lyons
2,991 PointsMy brother works for a track and trace company in an office. He asked me if i know the Javascript code to display the date 12 days after todays date. Below is what i came up with but it displays a different format and also shows the time which i don't want. I'm looking for the result just to be DD/MM/YYYY.
Dino Paškvan
Courses Plus Student 44,108 PointsTo get the desired output, you will need to reformat the date like this:
var formattedDate = dateInTwelveDays.getDate() + '/' + dateInTwelveDays.getMonth() + '/' + dateInTwelveDays.getFullYear();
This will store the formatted value into the formattedDate variable.
Then you can add it to the page by using something like this:
var dateTarget = document.getElementById("dateTarget");
dateTarget.innerText = formattedDate;
Darrel Lyons
2,991 PointsHmm. It doesn't seem to be working. It just comes up with a blank screen.
Dino Paškvan
Courses Plus Student 44,108 PointsWell, if your page doesn't have an element with the id dateTarget it won't work. I was giving you a general example of how it's done. Code for adding something to a page is not universal. It depends on the page itself.