0

I am working on a REST API, which return a field named modified in the following format:-

2018-02-23T00:25:12Z

While i want to show the modified in the following format:-

23/02/2018 00:25 

now inside my javascript i tried converting the modified value to string and provide a format as follow:-

items[i].Modified.toString("dd-MM-yyyy hh:mm");

but this did not change the date time format ??

John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

1

You can use the function toLocaleString.

var date = new Date('2018-02-23T00:25:12Z');
console.log(date.toLocaleString('es', {
  year: 'numeric',
  month: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  day: 'numeric',
  hour12: false
}))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Resource

Ele
  • 33,468
  • 7
  • 37
  • 75
  • but i want the date in the following format so the date should be `23/02/2018 00:25` instead of `02/23/2018 00:25`.. – John John Feb 23 '18 at 01:12
  • 1
    @johnG your format shows only numbers, so you can test any locale to format that date, i.e: `es`. See the updated answer. – Ele Feb 23 '18 at 01:18
  • 1
    The format of *toLocaleString* is implementation dependent, even using the Intl object, you can't guarantee the format of the overall string, only the parts of it. – RobG Feb 23 '18 at 02:01
  • 1
    @RobG the documentation is clear, for example: `US English uses month-day-year order` or `British English uses day-month-year order`, and so on. That means that a specific locale can modify how the date object is formatted. – Ele Feb 23 '18 at 02:10
  • 1
    @Ele—by "the documentation" I guess you mean MDN. However, it's not normative, it's a public wiki of useful information. The specification (ECMA-402) doesn't define what format is used for which language, it passes that responsibility the [*Unicode Common Locale Data Repository*](http://cldr.unicode.org/core-spec#Locale), which also notes that user preferences deviate from "locale" preferences and that such preferences change over time. So just because a particular implementation formats a date for a certain language a particular way doesn't mean all implementations will do the same. – RobG Feb 23 '18 at 02:21
  • 1
    @RobG right right, I got it. It makes sense. Have a good day! – Ele Feb 23 '18 at 09:30
  • @RobG now i tried this `var d = new Date(items[i].Modified.toString()); d = d.toLocaleString('en-GB', { year: 'numeric', month: '2-digit', hour: '2-digit', minute: '2-digit', day: 'numeric', hour12: false });` and looks like i got the format i want. something as follow `26 February 2018 02:07:42` .. so can i guarantee that this will always be the format ? – John John Feb 27 '18 at 14:31
  • @johnG—in Safari I get a string like "23/02/2018, 10:25" so no, there is absolutely no guarantee at all, quite the opposite. You've specified `month: '2-digit'` but shown it as 'long'. If you want a specific format, do it manually or use a library, you can't rely on the Int object for exact formatting, it's not intended for that. – RobG Feb 28 '18 at 03:49
0

I do not know how you do? But I hope my ways can help you

var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();

  var h = today.getHours();
  var m = today.getMinutes();

  if (dd < 10) {
    dd = '0' + dd;
} 

if (mm < 10) {
  mm = '0' + mm;
} 

var today = dd + '/' + mm + '/' + yyyy + ' ' + h + ':' + m;

DEMO :

<p id="demo"></p>

<script>
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();

  var h = today.getHours();
  var m = today.getMinutes();

  if (dd < 10) {
    dd = '0' + dd;
} 

if (mm < 10) {
  mm = '0' + mm;
} 

var today = dd + '/' + mm + '/' + yyyy + ' ' + h + ':' + m;
document.getElementById("demo").innerHTML = today;
</script>
theAlexandrian
  • 870
  • 6
  • 18
AirBlack
  • 135
  • 1
  • 9