-2

I have project which contain some historical data of some customers. Data contains some date column which has date like 7272017 (MMddyyyy). I've been able to convert this datetype by using c# utility by using following code.

DateTime dt=DateTime.ParseExact("12022017", "MMddyyyy",
                         System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dt);

But i want to create a utility in javascript to convert this date into ISODate. By using new Date(07272017) i am getting default date which is Thu Jan 01 1970 05:31:17 GMT+0500. How can i parse format to parse function and convert the date into other format?

This is not the same question as Converting a string to a date in JavaScript

i dont want to convert ANY string .. i want to convert a specific date format which i have done in other language but looking for solution in javascript.

[EDIT] Moment.js is workable option but i want to convert my existing records stored in mongodb. I know i can't do it with mongo query... i am using nosqlbooster so i have access to shell which can run javascript code.. i am not sure how to link moment.js there?

Ahsan Sohail
  • 660
  • 6
  • 17
  • The answers to the linked question provide multiple examples of how dates can be parsed, including ones that would work for your specific format. – Herohtar Nov 07 '18 at 18:49
  • updated the question please check if the link fulfill my scenario. – Ahsan Sohail Nov 07 '18 at 19:15
  • Everything you need is in the accepted answer to the other question. [Moment](https://momentjs.com/) is the easiest solution because it allows syntax very similar to what you had in C#, but if you can't use it for some reason the solution is to split the string using the appropriate [string methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Methods_2) and either reassemble the components into ISO format or use them individually in the constructor to get a `Date` object. – Herohtar Nov 07 '18 at 19:32

1 Answers1

0

If you can use moment I would advise you to do so.

var day = moment("12022017", "MMDDYYYY");

console.log(day);
<script src="https://momentjs.com/downloads/moment-with-locales.js"></script>

For loading external js into mongo shell, read this

Alfredo A.
  • 1,697
  • 3
  • 30
  • 43