我希望以相同的格式从系统获取系统本地日期/时间格式和显示日期。例如,如果系统日期格式是月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;更多推荐
如何获得本地日期/时间格式?
发布评论