如何获得本地日期/时间格式? [英] how to get local date/time format?
问题描述
我希望以相同的格式从系统获取系统本地日期/时间格式和显示日期。例如,如果系统日期格式是月dd / yyyy ,并且数据库中存储的日期是 mm / dd / yyyy 格式,那么我需要获取本地日期/时间格式并转换我的将日期存储为本地格式。
HTML5输入 type =date
这将采用相同格式的默认系统日期/时间格式和显示日期。如何使用 jquery
或 javascript
执行此操作。
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
.
推荐答案
您可以使用JavaScript 日期
对象:
You could use the JavaScript Date
object:
var date = new Date(yearFromDb, monthFromDb, dayFromDb);
date.toLocaleDateString();
编辑
上述方法似乎在不同浏览器(Chrome,IE,Firefox)之间不一致。因此,我认为您唯一的选择是从服务器端获取格式化的日期字符串。
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.
这是 jsFiddle ,显示了获取用户区域设置的跨浏览器方式,如果您可以使用此方法从后端获取当前区域设置中的格式化日期字符串(如果有) 。
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;
这篇关于如何获得本地日期/时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!