Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date object in dd/mm/yyyy hh:mm:ss format [duplicate]

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.

like image 780
Rihana Avatar asked Mar 17 '17 16:03

Rihana


People also ask

What is the difference between DD MM YYYY and HH?

In the object format dd is current day, mm is month, yyyy is year, HH is hour in 24-hour format, hh is hour in 12-hour format, MM is minutes, SS is seconds. The function formatData () takes an input and checks if it is greater than 9 or not.

How to format the current date in MM/DD/YYYY hh/mm ss format using NodeJS?

How to format the current date in MM/DD/YYYY HH:MM:SS format using Node.js? The current date can be formatted by using the Nodejs modules like Date Object or libraries like moment.js, dayjs . The JavaScript Date Object can be used in the program by using the following command.

How to print the date in MM/DD/YYYY HH/MMSS in 24-hour and 12-hour format?

The functions format24Hour () and format12Hour () prints the date in format MM/DD/YYYY HH:MM:SS in 24-hour and 12-hour format respectively. Run index.js file using below command: Method 2: Using Moment.js Library.

How to convert datetime string to%Y-%M-%D-%H%S format in Python?

%S means seconds. First the take DateTime timestamp as a String. Then, convert it into DateTime using strptime (). Now, convert into the necessary format of DateTime using strftime Example 1: Python program to convert DateTime string into %Y-%m-%d-%H:%M:%S format


1 Answers

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"));
like image 127
corn3lius Avatar answered Sep 16 '22 18:09

corn3lius