-1

I am not a very professional coder. I am just soo used to looking at them and I edit the very basics. First time into Javascript though. I have a website that i use yo code. It only allows to write css and html. Now i have created a comment section under that, also a Text called "Last Updated"

What I want is, I don't have to update the thread, but the Last Updated, automatically gets followed by today's date. Hence I have been searching for hours but failed to find. I need the format - Date Month Year [Example: 5 June 2016]

But heres the tough and challenging part. Also in the comments section I am trying to add dates that automatically update.So the top 2 latest comments are always on today's date, followed by the next 4-5 on yesterdays date, and the earlier 4-5 on the day before yesterday. Since the comments are also added by me, I need to keep those comments look recently added. The format would be same. Date Month Year. If anyone could help me.

Qoins
  • 1
  • 1
  • It seems you also want to know how to [*Add +1 to current date*](http://stackoverflow.com/questions/9989382/add-1-to-current-date/9989458#9989458). – RobG Jun 05 '16 at 05:57

1 Answers1

0

To get the current date and add to it in the desired format:

var current_date = new Date();
var month = current_date.getMonth() + 1;
var day = current_date.getDate();
var year = current_date.getFullYear();

// Add 4 days to the current day.
day += 4;

var formatted_date_string = day + '/' + month + '/' + year;

console.log(formatted_date_string);
ErikOlson186
  • 11
  • 1
  • 3
  • Why do you add one to the value returned by *getDate*? Why then add another 4 to that value? If the current day is say 31 May, this will return the day as 36 May. – RobG Jun 05 '16 at 05:56
  • I corrected the day value. Add 4 to it because he asked how to add to it. – ErikOlson186 Jun 05 '16 at 16:02
  • But that isn't the best way to add 4 days to a date, see [*Add +1 to current date*](http://stackoverflow.com/questions/9989382/add-1-to-current-date/9989458#9989458). ;-) – RobG Jun 05 '16 at 23:08
  • You're right. Just experimented and learned that `setDate` updates the month and year values too if the increase in days causes them to change! – ErikOlson186 Jun 06 '16 at 02:10