0

Just wanted a code how a UTC time converted to browser local time and vice versa dynamically without using moment.js needed only pure angular

Tried with Using AngularJS date filter with UTC date but i want dynamic conversion

HK123
  • 199
  • 2
  • 3
  • 17
  • Possible duplicate of [Convert UTC date time to local date time using JavaScript](http://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time-using-javascript) – Sachila Ranawaka Feb 17 '17 at 03:57
  • @sachilaranawaka—maybe, but the accepted answer is awful. – RobG Feb 17 '17 at 04:13
  • What is your "UTC time"? Is it a string? Number? Date? – RobG Feb 17 '17 at 04:14
  • @RobG its a Number – HK123 Feb 17 '17 at 04:48
  • @HK123—in that case, this is likely a duplicate of [*Best way to convert a unix timestamp to javascript date-time*](http://stackoverflow.com/questions/40083122/best-way-to-convert-a-unix-timestamp-to-javascript-date-time). There are also many answers for formatting Dates, see [*Where can I find documentation on formatting a date in JavaScript?*](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|20.2780) – RobG Feb 19 '17 at 22:22

2 Answers2

0

Add UTC at the end of the DateTime string before converting it to a date

example:

var date = new Date('2/16/2017 10:51:23 PM UTC');
console.debug(date.toLocaleTimeString()) // "5:51:23 PM"
joe_coolish
  • 7,201
  • 13
  • 64
  • 111
  • That relies on implementation dependent parsing, which is never a good idea. "UTC" is not a time zone. – RobG Feb 17 '17 at 04:15
  • @RobG While UTC is not a time zone, it does convert a UTC time to browser local time. I agree it's not the best solution, but DateTime is hard, and this gets the job done. How would you suggest converting between the 2 timezones? – joe_coolish Feb 17 '17 at 04:22
0

Here is how you can change the date to local and UTC.

var d = new Date();
document.getElementById("date").innerHTML = d;
document.getElementById("local").innerHTML = d.toLocaleTimeString();
document.getElementById("utc").innerHTML = d.toUTCString();
document.getElementById("fromutc").innerHTML = Date(d.toUTCString());
Date => <span id="date"></span><br>
Local => <span id="local"></span><br> 
UTC => <span id="utc"></span><br> 
FromUTC => <span id="fromutc"></span><br>

Also find here the reference of all the dateTime functions available in javascript

JavaScript Date Reference

ECMA Date Reference

MDN Date Reference

Manish
  • 4,692
  • 3
  • 29
  • 41
  • Please do not reference w3schools. Use [*ECMA-262*](https://www.ecma-international.org/ecma-262/7.0/index.html#sec-date-objects) as a normative reference and [*MDN*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) where further explanation and examples will help. – RobG Feb 17 '17 at 04:17
  • @Manish Thank you....it works.......it seems to be of javascript......is there a method we can do this stuff using angular(any angular related functinalities)?.... – HK123 Feb 17 '17 at 09:07
  • @HK123 no nothing similar to this is there is angular. Angular implements dates just as a filter[Date reference Angular](https://docs.angularjs.org/api/ng/filter/date) You have to do it using javascript and also angular is itself built on javascript so this should be fine. Apart from this i'm not aware of any angular module that handles dates (if exists) you might have to google may find one.. – Manish Feb 17 '17 at 09:13
  • @Manish—it would be better if you removed the w3schools link completely. The information presented has errors and has nothing to recommend it over MDN or ECMA-262, in my opinion of course. ;-) – RobG Feb 19 '17 at 22:25
  • While this does convert to local time, it is not using angularjs. – Paul Rooney Nov 13 '17 at 23:57