-2

I have tried moment.js, parsing it to string etc. I found no luck in converting it to my desired output.

I have this code

console.clear();

var date = [];
$.ajax({
    url: "https://api.myjson.com/bins/1dqpsd",
    dataType: "json",
    success: function (result) {
        //console.log(result); //Now a JSON object
        for (var i in result){
          //console.log(result[i]);
          date[i] =result[i].commit);
          console.log(date[i]);
        }
    }
});

This returns a format like

"2017-07-22T19:36:50.000+12:00"
"2017-07-22T14:46:40.000+12:00"
"2017-07-21T22:46:18.000+12:00"
"2017-07-20T19:32:10.000+12:00"

I want to be able to convert it to something like

July 22, 2017 6:36 PM
July 22, 2017 2:46 PM
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
  • 2
    Can you show us the moment.js code you tried to implement and the result you got? – Neil Girardi Sep 17 '17 at 23:31
  • Probably a duplicate of [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Sep 18 '17 at 01:03

2 Answers2

0

You can use toLocaleString with the format you have. But this assumes that the +12:00 is the right time zone for you.

For example:

var time = "2017-07-22T19:36:50.000+12:00"
var options = { year: 'numeric', month: 'long', day: 'numeric', hour: "2-digit", minute: "2-digit" };
var localstring = new Date(time).toLocaleString('en-US', options)
console.log(localstring)

// 'July 21, 2017, 11:36 PM'

That's correct for my time-zone, which is -08;

If you want the time in a different timezone you can include that in options:

var options = { year: 'numeric', month: 'long', day: 'numeric', hour: "2-digit", minute: "2-digit", timeZone: "America/New_York" };
new Date(time).toLocaleString('en-US', options)
// 'July 22, 2017, 3:36 AM'
Mark
  • 90,562
  • 7
  • 108
  • 148
  • 1
    The value returned by *toLocaleString* is implementation dependent and may or may not be in the format required by the OP. Minimal testing shows different results in different browsers, e.g. Safari returns "9/18/2017, 11:04:22 AM", Firefox returns "18/09/2017, 11:06:05 am". – RobG Sep 18 '17 at 01:02
  • @RobG Please look at the whole answer before commenting and down voting. You clearly didn't pass `toLocaleString` an `options` object like my example. In my tests: Firefox: July 21, 2017, 11:36 PM, Safari: July 21, 2017, 11:36 PM, Chrome: July 21, 2017, 11:36 PM, Node: July 21, 2017, 11:36 PM. `toLocaleString()` with `options` is well supported on modern browsers. – Mark Sep 18 '17 at 01:17
  • 1
    The same argument applies to using an options argument, though to a lesser extent. It just happens that the commonly used format is similar to the one the OP wants, but there is no guarantee. BTW, a timezone that uses +11:00 offset is 'Pacific/Guadalcanal'. ;-) I think the writers of ECMA 402 missed a good opportunity to provide a decent parser and formatter, e.g. currently the format is based on language, which it should not be. While it can be used for precise formatting, it's quite verbose (you essentially have to get the bits one at a time and then format them). – RobG Sep 18 '17 at 01:36
  • @RobG I'm not sure how the same argument applies. The above answer provides the OP precisely what they want with no third party libraries. It may be somewhat verbose, but it's not as verbose as 51KB, which is https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js comes in at. – Mark Sep 18 '17 at 01:52
  • I agree regarding moment.js, however the OP said they're using it, I wouldn't have answered with moment.js otherwise. I'm happy to reverse my vote if you include a caveat around *toLocaleString*, e.g. there's an extra comma in your examples. Trivial perhaps, but it highlights the difficulty of precise formatting using *toLocaleString*. Also, there's no standard mapping of language to format, nor are implementations required to support all languages. So *en-us* may be OK, but *to* (Tongan) returns "2017 M07 22 18:36" in Chrome and "22 Siulai 2017, 6:36 efiafi" in Firefox. – RobG Sep 18 '17 at 02:06
  • @RobG Perhaps it proves your point, but I'm having a hard time finding that extra comma you're talking about. – Mark Sep 18 '17 at 02:37
  • 1
    "July 22, 2017 6:36 PM" cf "July 21, 2017, 11:36 PM". – RobG Sep 18 '17 at 07:02
0

If you are using moment.js, then you can parse the string to a Date then format it using appropriate format tokens.

Note that moment.js parses ISO 8601 strings by default so you don't need to provide a parse format, however it's good practice to always provide the format to avoid incorrectly parsing malformed strings:

var s = "2017-07-20T19:32:10.000+12:00";

console.log(moment(s).format('MMMM DD, YYYY hh:mm a'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

The tokens for parsing and formatting are in the documentation.

RobG
  • 142,382
  • 31
  • 172
  • 209