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

Get Date/Timestamp in a particular format

本文关键字:日期 时间戳 格式 定格 获取      更新时间:2023-09-26

我想在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那样的简写。

也有什么简单的方法可以做到这一点。

提前感谢您的帮助

如果你使用第三方库没有任何问题,你可以使用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>

希望能有所帮助

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

失眠让我写下这个…

它在date对象中添加了一个format方法,而不需要任何第三方库,并从给定的简写中返回一个格式化的日期。

我基于PHP的日期简写,你可以参考一下。

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

和小提琴