Search code examples
javascriptarraysdatedate-format

JavaScript: How to get simple date without timezone


I have code that generates random dates in a date range, which gives me dates which, when logged, produce this format:

Wed Sep 25 2019 05:00:00 GMT+0500 (Pakistan Standard Time)

I just want to get the date without timezone and Day specifically like this:

2019-09-25

I am trying to get random dates between specified dates using the following code:

    var startDate = new Date("2019-08-26"); //YYYY-MM-DD
    var endDate = new Date("2019-09-25"); //YYYY-MM-DD

    var getDateArray = function(start, end) {
        var arr = new Array();
        var dt = new Date(start);
        while (dt <= end) {
            arr.push(new Date(dt));
            dt.setDate(dt.getDate() + 1);
        }
        return arr;
    }

    var dateArr = getDateArray(startDate, endDate);


    function shuffle(arra1) {
        var ctr = arra1.length, temp, index;

    // While there are elements in the array
        while (ctr > 0) {
           // Pick a random index
            index = Math.floor(Math.random() * ctr);
            // Decrease ctr by 1
            ctr--;
            // And swap the last element with it
            temp = arra1[ctr];
            arra1[ctr] = arra1[index];
            arra1[index] = temp;
        }
        return arra1; }

    console.log(shuffle(dateArr));

It's not a duplicate question as I was trying to achieve different and very specific formate.


Solution

  • One solution would be to map each item of arra1 through a custom formating function (ie formatDate()) where .getDate(), .getMonth() and .getYear() are used to populate the formatted string:

    function formatDate(date) {
      const year = date.getFullYear();
      /* getMonth returns dates from 0, so add one */
      const month = date.getMonth() + 1;
      const day = date.getDate();
    
      return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
    }
    

    Some points to consider here are:

    • Date#getMonth() returns 0-indexed dates in the range of 0-11. To match the desired date format, you should add 1 as shown
    • Check for day and month values that are less than 10 and prefix a 0 to pad those numbers to obtain the desired formatting

    This can be added to your existing code as shown:

    var startDate = new Date("2019-08-26"); //YYYY-MM-DD
    var endDate = new Date("2019-09-25"); //YYYY-MM-DD
    
    function formatDate(date) {
      const year = date.getFullYear();
      /* getMonth returns dates from 0, so add one */
      const month = date.getMonth() + 1;
      const day = date.getDate();
      
      return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
    }
    
    
    var getDateArray = function(start, end) {
      var arr = new Array();
      var dt = new Date(start);
      while (dt <= end) {
        arr.push(new Date(dt));
        dt.setDate(dt.getDate() + 1);
      }
      return arr;
    }
    
    var dateArr = getDateArray(startDate, endDate);
    
    
    function shuffle(arra1) {
      var ctr = arra1.length,
        temp, index;
    
      // While there are elements in the array
      while (ctr > 0) {
        // Pick a random index
        index = Math.floor(Math.random() * ctr);
        // Decrease ctr by 1
        ctr--;
        // And swap the last element with it
        temp = arra1[ctr];
        arra1[ctr] = arra1[index];
        arra1[index] = temp;
      }
    
      /* Update this line */
      return arra1.map(formatDate);
    }
    
    console.log(shuffle(dateArr));