最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get local datetime format? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jul 18, 2013 at 14:09 000 27.3k10 gold badges74 silver badges103 bronze badges asked Jul 15, 2013 at 5:59 PrasadPrasad 1,8127 gold badges23 silver badges29 bronze badges 5
  • 2 So what is your question? and what have you tried so far? – Dhaval Marthak Commented Jul 15, 2013 at 6:01
  • 1 I want to read local date/time format and display stored date in that format. I am not able to get system date format like mm/dd/yyyy or dd/mm/yyyy etc. – Prasad Commented Jul 15, 2013 at 6:03
  • stackoverflow./questions/8469436/… – Satpal Commented Jul 15, 2013 at 6:04
  • possible duplicate of JavaScript - Get system short date format – Pekka Commented Jul 15, 2013 at 6:05
  • 1 I hope you're not storing a date as a mm/dd/yyyy string in your database. You should be storing it as a date (or datetime) - the ANSI SQL format for these is YYYY-MM-DD (of course this does not address your question about locale settings) – Stephen P Commented Jul 15, 2013 at 6:26
Add a ment  | 

1 Answer 1

Reset to default 1

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;
发布评论

评论列表(0)

  1. 暂无评论