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.
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 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.
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.
%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
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With