在javascript格式化日期值 [英] Formatting date value in javascript

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

问题描述

HTML

<input type="datetime-local" onblur="window.setValue(this.value)" />

JS

window.setValue = function (val) {
    console.log(val);
}

上面的输出是 1991-03-02T00: 01 ,如何获得确切的值?像 03/02/1991 12:01 AM

The output above is 1991-03-02T00:01, how to get the exact value? Like 03/02/1991 12:01 AM.

推荐答案

很简单,只需使用javascript,如@chrana所示。还有一些库使用javascript的本机 Date 对象并允许格式化, moments.js 是其中之一。我也在图书馆工作,也可以使用标准的 CLDR符号 ,但不依赖于 Date ,而是以纯数学方式完成所有操作,以准确进行天文学约会。

It is pretty easy to do using just javascript, as demonstrated by @chrana. There are also a number of libraries which use javascript's native Date object and allow formatting, moments.js is one of them. I have also been working on a library which will also allow formatting dates by using standard CLDR notation but does not rely on Date, instead everything is done in pure math, for accurate astronomy dating.

您所显示的格式非常类似于标准的美国短日期,除非您没有

The format that you have shown is very similar to the standard US short date, except you have no ,

03/02/1991, 12:01 AM

但是使用我的图书馆和任何使用CLDR符号的图书馆可以这样做。

But using my library and any library using CLDR notation it could be done like this.

MM/dd/Y h:mm a

require.config({
    paths: {
        'astrodate': '//rawgit.com/Xotic750/astrodate/master/lib/astrodate'
    }
});

require(['astrodate'], function (AstroDate) {
    "use strict";
    
    var date = new AstroDate('1991-03-02T00:01');

    document.body.appendChild(document.createTextNode(date.format('MM/dd/Y h:mm a')));
});

<script src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script>

这篇关于在javascript格式化日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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