javascript - Display date/time in user's locale format and time offset -


i want server serve dates in utc in html, , have javascript on client site convert user's local timezone.

bonus if can output in user's locale date format.

seems foolproof way start utc date create new date object , use setutc… methods set date/time want.

then various tolocale…string methods provide localized output.

example:

// come server.  // also, whole block made mktime function.  // bare here quick grasping.  d = new date();  d.setutcfullyear(2004);  d.setutcmonth(1);  d.setutcdate(29);  d.setutchours(2);  d.setutcminutes(45);  d.setutcseconds(26);    console.log(d);                        // -> sat feb 28 2004 23:45:26 gmt-0300 (brt)  console.log(d.tolocalestring());       // -> sat feb 28 23:45:26 2004  console.log(d.tolocaledatestring());   // -> 02/28/2004  console.log(d.tolocaletimestring());   // -> 23:45:26

some references:


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -