获取特定格式的日期/时间戳
Get Date/Timestamp in a particular format
我想在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")
和小提琴
相关文章:
- JavaScript JSON关联对象键和值都作为日期时间戳
- 用时间戳获取两个不同日期之间的时间差
- 当转换为Python日期时间时,Node的时间戳落后3分钟
- 我想以时间戳的形式将日期设置为无限
- 时间戳日期&时间格式-应用程序脚本-设置日期和时间格式
- 将正常日期转换为 unix 时间戳
- 时刻.js + 实时戳时间前基于 UTC 来自服务器的日期时间
- 在谷歌图表折线图的x轴上显示日期,而不是时间戳
- JavaScript:获取MySQL时间戳和当前日期之间的天数差
- 如何在JS中从Y-m-d日期获取unix时间戳
- JavaScript将人类可读的日期转换为unix时间戳,然后再转换回来
- 使用 JavaScript 的客户端日期/时间戳一致(考虑时区)
- ArangoDB Foxx 模型日期时间戳不起作用
- 如何从日期/时间戳中减去两个不同的日期
- 获取日期+时间戳
- 如何在每个新条目中包含日期/时间戳
- 用javascript将unix日期时间戳转换为特定时区的时间
- 获取特定格式的日期/时间戳
- 存储过期日期/时间戳的正确方法是什么?
- 什么'这是允许用户在html/javascript中指定一系列日期/时间戳的最干净的方法