javascript 像 $.datepicker.formatDate 这样的函数来格式化时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18326810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:30:07  来源:igfitidea点击:

Function like $.datepicker.formatDate for formatting time?

javascriptjquery

提问by Click Upvote

With $.datepicker.formatDateI can only format a date, meaning if I pass in a Date object, I can only get the date part of it formatted but not the time part.

因为$.datepicker.formatDate我只能格式化日期,这意味着如果我传入一个 Date 对象,我只能格式化它的日期部分,而不能格式化时间部分。

Is there any equivalent function which allows the time part to also be formatted/parsed, preferably in the same syntax?

是否有任何等效的函数允许时间部分也被格式化/解析,最好使用相同的语法?

I'm using the following datetime plugin:

我正在使用以下日期时间插件:

https://github.com/eternicode/bootstrap-datepicker

https://github.com/eternicode/bootstrap-datepicker

But it doesn't seem to have an equivalent formatDate function that I can use.

但它似乎没有我可以使用的等效 formatDate 函数。

回答by Arun P Johny

The datepicker's formatDate()method does not have any option to format time, for that look at a datetime library like moment.js

datepicker 的formatDate()方法没有任何格式化时间的选项,为此请查看像moment.js这样的日期时间库

Ex

前任

var date = new Date();

var df = moment(date).format('MMMM Do YYYY, h:mm:ss a');
alert(df)

and

var d = moment('2013-09-11 14:44:03', 'YYYY-MM-DD HH:mm:ss');
console.log(d.format('M/D/YYYY h:m A'))

Demo: Fiddle

演示:小提琴