DMS-KH
DMS-KH

Reputation: 2787

How to generate date from current days to last days in Javascript?

I want to generate date as my below PHP code in Javascript But I don't know how to do.

$begin2 = new DateTime(date("Y-m-d", strtotime("-5 day")));
$interval2 = new DateInterval('P1D');
$end2 = new DateTime(date("Y-m-d", strtotime("+1 day")));
$daterange2 = new DatePeriod($begin2, $interval2, $end2);
foreach (array_reverse(iterator_to_array($daterange2)) as $val) {
echo $val->format("Ymd");
}
Output:
2015-12-04
2015-12-03
2015-12-02
2015-12-01
2015-11-30
2015-11-29
2015-11-28
2015-11-27
2015-11-26
2015-11-25

Upvotes: 1

Views: 77

Answers (3)

RobG
RobG

Reputation: 147343

Edit

Wow, completely missed the point of the question!

Seems you want dates from today going backwards for a set number of days in ISO 8601 format. The Date constructor will create a date, and Date.prototype.toISOString will return an ISO 8601 date. It just needs the time part trimmed.

So a function to returns date strings for all the dates from today going back n days is:

function getDateRange(n) {
  var d = new Date(),
      dates = [];
 
  while (n--) {
    dates.push(d.toISOString().split('T')[0]);
    d.setDate(d.getDate() - 1);
  }
  return dates;
}

// Example
document.write(getDateRange(10).join('<br>'));

Original answer

The only reliable way to parse date strings in javascript is to do it manually. A library can help, but a bespoke function isn't much work:

function parseYMD(s) {
  var b = s.split(/\D/);
  return new Date(b[0], b[1]-1, b[2]);
}

document.write(parseYMD('2015-12-04'))

This assumes the string is a valid date and will parse the string to a local Date, consistent with ECMAScript 2015 (and ISO 8601). If you need to also validate the string, a couple of extra lines are required.

Upvotes: 1

onerror
onerror

Reputation: 616

Here's the code doing an iteration in reverse order for your given dates

 var now = new Date();
    var begin2 = new Date();
    var end2 = new Date();
    var year, month, day, datestr;
    begin2.setDate(now.getDate() - 5);
    end2.setDate(now.getDate() + 1);
    var current = begin2;
    var resulting_dates = [];
    while (current <= end2) {
      datestr = current.getFullYear() + '-' + ('0' + (current.getMonth() + 1)).slice(-2) + '-' + ('0' + current.getDate()).slice(-2);
      resulting_dates.push(datestr);
      current.setDate(current.getDate() + 1);
    }
    console.log(resulting_dates);

Upvotes: 1

Kursad Gulseven
Kursad Gulseven

Reputation: 2008

Native "Date" will be enough for some date operations.

var myDate = new Date();
var dateLate = new Date();
var dateEarly = new Date();

dateLate.setDate(dateLate.getDate() + 10);
dateEarly.setDate(dateEarly.getDate() - 10);

myDate.setDate(dateLate.getDate());
while (myDate.getDate() != dateEarly.getDate()) {
    myDate.setDate(myDate.getDate() - 1);
    document.write(myDate.toLocaleDateString() + '<br>');
}

You can format the date in a different way.

Upvotes: 1

Related Questions