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"
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.