Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get local date/time format?

I want to get systems local date/time format and display date from database on this same format. e.g if system date format is like Month dd/yyyy and date stored in database is mm/dd/yyyy format then I need to get local date/time format and convert my stored date into local format. HTML5 input type="date" this takes default systems date/time format and show date in same format. how can I do this with jquery or javascript.

like image 292
Prasad Avatar asked Jul 15 '13 05:07

Prasad


People also ask

How can I format local date?

LocalDate is an immutable class that represents Date with default format of yyyy-MM-dd. We can use now() method to get the current date.

What is local time format?

A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30 . LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision. For example, the value "13:45.30. 123456789" can be stored in a LocalTime .

Does local date have timezone?

For example, the value "2nd October 2007" can be stored in a LocalDate . This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

How do you initialize local date-time?

The easiest way to create an instance of the LocalDateTime class is by using the factory method of(), which accepts year, month, day, hour, minute, and second to create an instance of this class. A shortcut to create an instance of this class is by using atDate() and atTime() method of LocalDate and LocalTime class.


1 Answers

You could use the JavaScript Date object:

var date = new Date(yearFromDb, monthFromDb, dayFromDb);
date.toLocaleDateString();

EDIT

It seems that the above method is not consistent between different browsers (Chrome, IE, Firefox). Therefore, I think your only option is to get the formatted date string from server side.

Here is a jsFiddle that shows a cross-browser way of getting the users locale, if you can use this to get a formatted date string in the current locale from the backend (if any).

var locale = window.navigator.userLanguage || window.navigator.language;
like image 89
huysentruitw Avatar answered Oct 26 '22 23:10

huysentruitw