0

Problem Question: I have a date string(could be in any format 31/10/93 or 10/31/93 or 93/10/31) and want to convert this to date object(UTC format).

how to do this using momentjs ?

what I tried moment.utc(date) but no result.

Any suggestion on how to do this?

1

2 Answers 2

2

You can parse string with multiple formats as described here.

If you need you can also pass locale parameter.

The following code shows a possible solution to your question:

var arr = ['31/10/93','10/31/93','93/10/31'];

for(var i=0; i< arr.length; i++){
  var m = moment.utc(arr[i], ['DD/MM/YY', 'MM/DD/YY', 'YY/MM/DD']);
  console.log(m.format('DD MMMM YYYY')); // 31 October 1993
}
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>

This works fine for the input you gave as example, but may give you unexepcted results for inputs like 01/02/93. This string can represent both 1 February 1993 and 2 January 1993.

The suggested code will show 01 February 1993 because, as stated in the docs:

Moment uses some simple heuristics to determine which format to use. In order:

  • Prefer formats resulting in valid dates over invalid ones.
  • Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
  • Prefer formats earlier in the array than later

If you need a JavaScript Date from moment object you can use toDate() method, while if you need Unix timestamp you can use valueOf() or unix().


After your comment, I can suggest to get "localized" format using moment.localeData(). Using longDateFormat('L') you can get localized format string and then use it in the parsing function. For example, longDateFormat('L') will return MM/DD/YYYY for us locale and DD/MM/YYYY for it. Code sample:

var s = '01/02/93';

function formatSample(s){
  var ldf = moment.localeData().longDateFormat('L');
  return moment.utc(s, [ldf, 'YY/MM/DD']).format('DD MMMM YYYY');
}

moment.locale('us');
formatSample(s); // 02 January 1993
moment.locale('it');
formatSample(s); // 01 febbraio 1993
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment-with-locales.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is close enough! But i wish moment has something where it just look at the moment.local() and identify what format to use for the date to convert to UTC.
@GeekOnGadgets I've updated my answer trying to give you what you explained in your comment.
how do you change 2 February 2016 to user date format using local? like DD/MM/YYYY or MM/DD/YYYY or YYYY/MM/DD
0

if you don't know the format, than you can't. you must provide a format for the moment constructor because 1/2/93 can be first of feb or second of jan. there is no way for moment.js to decide this for you correctly.

See following section for more info

http://momentjs.com/docs/#/parsing/string-format/

2 Comments

can moment take format dynamically ? by passing the local ?
you give it the format each time you want it to parse the string and convert to moment object as follows: moment("12-25-1995", "MM-DD-YYYY");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.