如何使用moment.js查找日期格式(不针对特定格式进行验证) [英] How to find format (not validate against a particular format) of date with moment.js

查看:72
本文介绍了如何使用moment.js查找日期格式(不针对特定格式进行验证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个给定的Date字符串,该字符串已经由moment.js格式化.我想要那种格式.

I have a given Date string, which is already formatted by moment.js by someone. I want to get that format.

我检查了现有的问题,并遇到了这个问题-用moment.js检查日期格式

I have checked existing questions and came across this one - check format of date with moment.js

提供的解决方案未返回日期格式.而是针对某些给定格式进行验证.

The solution provided is not returning the format of the date. Rather it is validating against some given formats.

我想要这样的函数getMomentDateFormat-

I want a function getMomentDateFormat like this -

var dateString = field.value; //"Nov 30, 2017"
var dateFormat = getMomentDateFormat(dateString ); //I want dateFormat = "MMM DD, YYYY"

推荐答案

您可以使用解析日期格式插件:

此插件提取日期/时间字符串的格式.

This plugin extracts the format of a date/time string.

这里有个例子(无论如何,它返回 MMM D,YYYY 而不是 MMM DD,YYYY ):

Here an example (anyway it returns MMM D, YYYY instead of MMM DD, YYYY):

function getMomentDateFormat(input){
  return moment.parseFormat(input);
}

var field = { value: "Nov 30, 2017" };
var dateString = field.value; //"Nov 30, 2017"
var dateFormat = getMomentDateFormat(dateString);

console.log(dateFormat);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
<script src="https://gr2m.github.io/moment-parseformat/moment-parseformat.js"></script>

PS.不要直接从 https://gr2m.github.io/使用脚本,请参考

PS. Do not use scripts directly from https://gr2m.github.io/, refer github repo.

这篇关于如何使用moment.js查找日期格式(不针对特定格式进行验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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