Reputation: 1075
in my nodejs app i need date in the Y-m-d H:i:s
format , i use this simple code
console.log(new Date().toLocaleString());
in the local computer i get
2019-1-8 04:14:28
which is the correct format but the same code in the live server gives me 1/8/2019, 4:14:00 AM
which is not what i want .... why is that and how can i fix that ?
Upvotes: 2
Views: 4091
Reputation: 1118
From MDN web docs:
The toLocaleString() method returns a string with a language sensitive representation of this date. The new locales and options arguments let applications specify the language whose formatting conventions should be used and customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent.
Example:
var event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// British English uses day-month-year order and 24-hour time without AM/PM
console.log(event.toLocaleString('en-GB', { timeZone: 'UTC' }));
// expected output: 20/12/2012, 03:00:00
// Korean uses year-month-day order and 12-hour time with AM/PM
console.log(event.toLocaleString('ko-KR', { timeZone: 'UTC' }));
// expected output: 2012. 12. 20. 오전 3:00:00
You are not passing the location parameter to toLocaleString
, so the current location will be used. You see a different output on your machine vs. remote server because they are physically located in different countries.
Upvotes: 4
Reputation: 815
You can use moment.js for date and time manipulations.
moment("2010-10-20 4:30:12", "YYYY-MM-DD HH:mm:ss"); // parsed as 4:30:12 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
More info can be found at the Moment String Format.
Upvotes: 0