chintan310
chintan310

Reputation: 394

How to get the Local Time on a server in a different timezone?

I am from India and I have a shared server with GoDaddy in the US for hosting an ASP.net website.

When I Use DateTime.Now property I get the time in US.

How can I change it to Indian time for whole my application?

Upvotes: 1

Views: 1828

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241808

Using TimeZoneInfo is the best approach:

var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZoneInfo);

Upvotes: 4

competent_tech
competent_tech

Reputation: 44961

I don't believe there is a way to change the timezone for an application, but you could replace all instances of DateTime.Now with a static class method that gets the current date and then adjusts it for the desired time zone.

For example:

        var dtDate = DateTime.UtcNow;
        dtDate.AddHours(5);
        dtDate.AddMinutes(30);
        return dtDate;

Upvotes: 0

Related Questions