← Back to challenges

Time Around the World

JavaScriptHarddatesformatting

Instructions

In this challenge, the goal is to calculate what time it is in two different cities. You're given a string cityA and the related string timestamp (time in cityA) with the date formatted in full U.S. notation, as in this example:

"July 21, 1983 23:01"

You have to return a new timestamp with date and corresponding time in cityB, formatted as in this example:

"1983-7-21 23:01"

See the table below for a list of given cities and their GMT (Greenwich Mean Time) hours offsets.

GMTCity
- 08:00Los Angeles
- 05:00New York
- 04:30Caracas
- 03:00Buenos Aires
00:00London
+ 01:00Rome
+ 03:00Moscow
+ 03:30Tehran
+ 05:30New Delhi
+ 08:00Beijing
+ 10:00Canberra

Examples

timeDifference("Los Angeles", "April 1, 2011 23:23", "Canberra") ➞ "2011-4-2 17:23"
// Can be a new day.

timeDifference("London", "July 31, 1983 23:01", "Rome") ➞ "1983-8-1 00:01"
// Can be a new month.

timeDifference("New York", "December 31, 1970 13:40", "Beijing") ➞ "1971-1-1 02:40"
// Can be a new year.

Notes

  • Pay attention to hours and minutes, a leading 0 is needed in the returned timestamp when they're a single digit (see examples #2 and #3).
  • Pay attention to cities with half hours offsets.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.