获取特定格式的日期/时间戳 [英] Get Date/Timestamp in a particular format

查看:335
本文介绍了获取特定格式的日期/时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以javascript中的以下格式 16-SEP-16 12.00 获取日期。我不太清楚如何实现这一目标。我需要将其作为REST API请求的查询参数发送。

I want to get Date in the for following format 16-SEP-16 12.00 in javascript. I can't quite figure out how to achieve this. I need to send this as a query parameter for a REST API request.

当前,我所能想到的就是这样分别格式化日期

Currently all I can think of this to format the date individually like

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

我有一个问题,那就是月份是数字而不是像 SEP

I have a problem with it that the month is a number and not a shorthand like SEP.

也可以通过任何简单的方法来实现。

Also is there any simple way to achieve this.

预先感谢您的帮助。

推荐答案

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

If you dont have any problem using third party library, you can use Moment.js,it is a beautiful library for dates and formatting

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

In moment.js,you can format it as the following

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>

希望这会有所帮助

这篇关于获取特定格式的日期/时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆