How to find format (not validate against a particu

2019-08-16 13:31发布

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

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.

I want a function getMomentDateFormat like this -

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

1条回答
冷血范
2楼-- · 2019-08-16 13:58

You can use Parse Date Format plug-in:

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

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. Do not use scripts directly from https://gr2m.github.io/, refer github repo.

查看更多
登录 后发表回答