如何获得本地日期/时间格式?

编程入门 行业动态 更新时间:2024-10-25 09:31:28
本文介绍了如何获得本地日期/时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望以相同的格式从系统获取系统本地日期/时间格式和显示日期。例如,如果系统日期格式是月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;

更多推荐

如何获得本地日期/时间格式?

本文发布于:2023-10-23 00:03:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何获得   日期   格式   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!