以特定格式获取日期/时间戳

时间:2016-11-02 04:18:24

标签: javascript date

我希望在javascript中使用以下格式16-SEP-16 12.00获取日期。我无法弄清楚如何实现这一目标。我需要将此作为REST API请求的查询参数发送。

目前我只能想到这个来单独格式化日期,如

var date = new Date();
var dt = date.getUTCDate() + '-' + date.getUTCMonth() + '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);

我有一个问题,即月份是一个数字,而不是像SEP这样的速记。

也有任何简单的方法来实现这一目标。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)



var monthNames = [
  "Jan", "Feb", "Mar",
  "Apr", "May", "Jun", "Jul",
  "Aug", "Sep", "Oct",
  "Nov", "Dec"
];


var date = new Date();
var dt = date.getUTCDate() + '-' + monthNames[date.getUTCMonth()] + '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);




不使用月份数组,您可以使用以下解决方案



var date = new Date();
Date.prototype.monthName = function() {
    return this.toUTCString().split(' ')[2]
};
var dt = date.getUTCDate() + '-' + date.monthName()+ '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);
document.write(dt);




答案 1 :(得分:1)

如果您在使用第三方库时没有任何问题,可以使用Moment.js,它是一个漂亮的日期和格式库

在moment.js中,您可以将其格式化为以下



var day = moment("2016-11-01");
console.log(day);
console.log(day.format("DD-MMM-YYYY HH:MM"));

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.js"></script>
&#13;
&#13;
&#13;

希望这有帮助

答案 2 :(得分:1)

失眠让我写下这个......

它向日期对象添加format方法,不需要任何第三方库,并从给定的速记返回格式化日期。

我将其基于PHP's date shorthand,您可以参考。

Date.prototype.format = function(format) {
  var months = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ];
  var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  format = format.replace(/y/g, ("" + this.getFullYear()).substring(2));
  format = format.replace(/Y/g, "" + this.getFullYear());
  format = format.replace(/m/g, ("00" + (this.getMonth() + 1)).substr(-2, 2));
  format = format.replace(/F/g, months[this.getMonth()]);
  format = format.replace(/M/g, months[this.getMonth()].substring(0, 3));
  format = format.replace(/n/g, "" + (this.getMonth() + 1));
  format = format.replace(/t/g, "" + new Date(this.getFullYear(), this.getMonth() - 1, 0).getDate());
  format = format.replace(/D/g, days[this.getDate()].substr(0, 3));
  format = format.replace(/d/g, ("00" + this.getDate()).substr(-2, 2));
  format = format.replace(/j/g, this.getDate()+"");
  format = format.replace(/l/g, days[this.getDate()]);
  format = format.replace(/w/g, this.getDay());
  format = format.replace(/a/g, this.getHours() > 11 ? "pm" : "am");
  format = format.replace(/A/g, this.getHours() > 11 ? "PM" : "AM");
  format = format.replace(/g/g, "" + (this.getHours() > 11 ? this.getHours() - 11 : this.getHours() + 1));
  format = format.replace(/G/g, "" + (this.getHours() + 1));
  format = format.replace(/h/g, ("00" + (this.getHours() > 11 ? this.getHours() - 11 : this.getHours() + 1)).substr(-2, 2));
  format = format.replace(/H/g, ("00" + (this.getHours() + 1)).substr(-2, 2));
  format = format.replace(/i/g, ("00" + this.getMinutes()).substr(-2, 2));
  format = format.replace(/s/g, ("00" + this.getSeconds()).substr(-2, 2));
  return format;
};

<强>示例..

// 11/02/16 2:17 AM
var d = (new Date()).format("m/d/y g:i A");

// November 2, 2016, 2:17 am
var d = (new Date()).format("F j, Y, g:i a")

And a fiddle