Link to home
Start Free TrialLog in
Avatar of TekiSreelakshmi
TekiSreelakshmi

asked on

Newyork,London,HongKong current time on my aspx page

Hi,

I want to display Newyork,London,HongKong current time on my aspx page.
How to do?

Plz advise.

Thanks
Sreelakshmi.N.
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Check this, hope that helps: http://www.earthtools.org/webservices.htm#timezone

Basically it's a free web service which you can call with any latitude/longitude and it returns the local time for that location.
You should use UTC time as base:
const int newYorkUtcShift = -4;
DateTime newYorkTime = DateTime.UtcNow.AddHours(newYorkUtcShift);

Open in new window

Avatar of TekiSreelakshmi
TekiSreelakshmi

ASKER

I need to consider DST also right ...
-4 or -5 ?
How to check..
Can u send me the complete code...


Thanks,
Sreelakshmi.
hi

DateTime.UtcNow
returns the datetime on the hosting server expressed in Universal Coordinated Time, otherwise known as Greenwich Mean Time.


DateTime.Now
returns the local datetime.

You get the offset in hours and minutes for each of the countries in your list (for e.g. India is 5 hrs 30 mins ahead of GMT) and add it to DateTime.UtcNow to get the local time of the countries.

DateTime localTimeIndia = DateTime.UtcNow.Add(new TimeSpan(5, 30, 0));
You can see time zones here: http://www.worldtimezone.com/
Create mmm.. e.g. dictionary of type <string, int> with city name as key and time zone as value.
Add cities that you need:
dictionary.Add("London", 0);
dictionary.Add("New York", -5);
dictionary.Add("Tokyo", +9);

Then you can quickly get local time for some city (it you have added one)
DateTime.UtcNow.AddHours(dictionary["Tokyo"])

GL
It is not working as u said...
If i add -5 to New york time it is showing wrong time ..
Day light saving is not considered in that case...



DateTime localTimeIndia = DateTime.UtcNow.Add(new TimeSpan(5, 30, 0));
problem here is it is not getting refreshed for every second..

So i am trying to use Javascript ...

ASKER CERTIFIED SOLUTION
Avatar of rajeeshmca
rajeeshmca
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok. You shold use TimeZoneInfo. Code below shows you how to get time zones and current local time for any time zone.

All you need is create mapping from time zone id to city name. E.g. dictionary with key as city name and value as time zone id. After that you simply do this:

string centralEuropeTimeZoneId = "Central Europe Standard Time";
Console.WriteLine(TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, centralEuropeTimeZoneId));
foreach (TimeZoneInfo zoneInfo in TimeZoneInfo.GetSystemTimeZones())
{
    DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, zoneInfo);
    Console.WriteLine("{2} - {0} ({1})", zoneInfo.Id, zoneInfo.BaseUtcOffset, localTime);
}

Open in new window