簡體   English   中英

JavaScript:如何獲得沒有時區的簡單日期

[英]JavaScript: How to get simple date without timezone

我有在日期范圍內生成隨機日期的代碼,它給了我日期,當記錄時,產生這種格式:

2019 年 9 月 25 日星期三 05:00:00 GMT+0500(巴基斯坦標准時間)

我只想獲得沒有時區和日期的日期,具體如下:

2019-09-25

我正在嘗試使用以下代碼在指定日期之間獲取隨機日期:

    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));

這不是一個重復的問題,因為我試圖實現不同且非常具體的格式。

一種解決方案是通過自定義格式化函數(即formatDate() )映射arra1每個項目,其中.getDate().getMonth().getYear()用於填充格式化字符串:

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}`
}

這里需要考慮的一些要點是:

  • Date#getMonth() 返回0-11 范圍內的 0 索引日期 要匹配所需的日期格式,您應該添加1 ,如圖所示
  • 檢查小於10日和月值並添加0前綴以填充這些數字以獲得所需的格式

這可以添加到您現有的代碼中,如下所示:

 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));

使用.toISOString().substr() 例子:

 var dt = new Date("2019-09-25"); console.log(dt.toISOString().substr(0,10)); // 2019-09-25

這種方法的優點是Date對象內置了.toISOString()方法,因此您不必重新發明輪子。 但是,該方法返回完整的 ISO 字符串,例如“2019-09-25T00:00:00.000Z”。 因此,您可以使用.substr僅檢索您想要使用的部分。

var getDates = function(startDate, endDate) {
    var dates = [],
        currentDate = startDate,
        addDays = function(days) {
            var date = new Date(this.valueOf());
            date.setDate(date.getDate() + days);

            return date;
    };

    while (currentDate <= endDate) {
        dates.push(currentDate);
        currentDate = addDays.call(currentDate, 1);
   }

        return dates;
    };

// Usage 
var dates = getDates(new Date(2019, 10, 22),
    new Date(2019, 11, 25));
dates.forEach(function(date) {
    console.log(date);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM