admin管理员组

文章数量:1290399

I am trying to convert format of date using Javascript. I found a method called toLocaleFormat.

 <script>
  var today = new Date();
  var formatted_string = today.toLocaleFormat('%d/%m/%Y at %H:%M:%S %p (%Z)');
  document.write(formatted_string);
 </script>

But its working only in firefox. I want to know an alternate method for this, which will work on all the browsers. Kindly help me to do this. Thanks in advance.

I am trying to convert format of date using Javascript. I found a method called toLocaleFormat.

 <script>
  var today = new Date();
  var formatted_string = today.toLocaleFormat('%d/%m/%Y at %H:%M:%S %p (%Z)');
  document.write(formatted_string);
 </script>

But its working only in firefox. I want to know an alternate method for this, which will work on all the browsers. Kindly help me to do this. Thanks in advance.

Share Improve this question asked Aug 20, 2015 at 11:22 Vijikumar MVijikumar M 3,7645 gold badges34 silver badges58 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

JavaScript in itself doesnt have advanced parse and formatting functions for dates. Most of the time we depend on framework we are using in application or any date based plugins like this one

http://momentjs./

To format dateObject

moment(dateObject).format('MMMM Do YYYY, h:mm:ss a'); // August 20th 2015, 5:09:08 pm

you can use toLocaleDateString() to get "dd.mm.yyy" format date

Example:var date = new Date().toLocaleDateString();

You can use a library if you really want to keep using that method of specifying the date format (using a format string, inherited from a similar C language function), but it is important to consider that that whole way of formulating dates is deprecated in js.

The remended alternative is 'Intl.DateTimeFormat' which is not a direct replacement - it does not include any way to explicitly specify a date format as a string, which is probably the idea (this allows more leeway for the system to formulate a representation suited to the user).

Also consider that you could still build a date with an explicit format by manually concatenating the ponents, but that is certainly more cumbersome and less dynamic.

本文标签: javascriptWhat is the alternate of toLocaleFormat for ChromeStack Overflow