17

I have a datetime object and its value is as follows

2017-03-16T17:46:53.677

Can someone please let me know how to convert this to dd/mm/yyyy hh:mm:ss format I googled a lott and could not find format conversion for this particular input.

blackmiaool
  • 5,234
  • 2
  • 22
  • 39
Rihana
  • 403
  • 2
  • 7
  • 14

2 Answers2

33

You can fully format the string as mentioned in other posts. But I think your better off using the locale functions in the date object?

var d = new Date("2017-03-16T17:46:53.677"); 
console.log( d.toLocaleString() ); 

edit :

ISO 8601 ( the format you are constructing with ) states the time zone is appended at the end with a [{+|-}hh][:mm] at the end of the string.

so you could do this :

var tzOffset = "+07:00" 
var d = new Date("2017-03-16T17:46:53.677"+ tzOffset);
console.log(d.toLocaleString());
var d = new Date("2017-03-16T17:46:53.677"); //  assumes local time. 
console.log(d.toLocaleString());
var d = new Date("2017-03-16T17:46:53.677Z"); // UTC time
console.log(d.toLocaleString());

edit :

Just so you know the locale function displays the date and time in the manner of the users language and location. European date is dd/mm/yyyy and US is mm/dd/yyyy.

var d = new Date("2017-03-16T17:46:53.677");
console.log(d.toLocaleString("en-US"));
console.log(d.toLocaleString("en-GB"));
corn3lius
  • 4,857
  • 2
  • 31
  • 36
  • @com3lius- this works fine. i can get the format right with this. but my local time is 11.30 am and it shows 4.30am. How can i add offset of 7 hours to this above code of yours ? – Rihana Mar 17 '17 at 18:37
  • The result of *toLocaleString* is entirely implementation dependent, it returns different results in different hosts. There is no way to guarantee it will fit the OP's format. – RobG Mar 18 '17 at 05:00
  • @RobG - totally agree its dependant on how the locale views dates. I did notice that my locale doesn't show as the OP requested `mm/dd` as opposed to `dd/mm`. But thats the normal way U.S. reads dates. – corn3lius Mar 20 '17 at 13:32
  • @corn3lius—I get m/d or d/m depending on which browser I use, so I have no confidence that system settings will be honoured (they *should* be used by default). The ECMA-402 "locale" is a misnomer, it's actually language. – RobG Mar 20 '17 at 20:40
  • This is actually outputting 16/03/2017, 17:46:53 but we are in 2023. So not working. – Raul Chiarella May 04 '23 at 20:45
  • its not printing date Now() its a predefined data from a string `var d = new Date("2017-03-16T17:46:53.677");` @RaulChiarella – corn3lius May 05 '23 at 17:09
10

Here we go:

var today = new Date();
var day = today.getDate() + "";
var month = (today.getMonth() + 1) + "";
var year = today.getFullYear() + "";
var hour = today.getHours() + "";
var minutes = today.getMinutes() + "";
var seconds = today.getSeconds() + "";

day = checkZero(day);
month = checkZero(month);
year = checkZero(year);
hour = checkZero(hour);
minutes = checkZero(minutes);
seconds = checkZero(seconds);

console.log(day + "/" + month + "/" + year + " " + hour + ":" + minutes + ":" + seconds);

function checkZero(data){
  if(data.length == 1){
    data = "0" + data;
  }
  return data;
}
Edric
  • 24,639
  • 13
  • 81
  • 91
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32