1

I have below code which shows Wishing message to user as per their current local time. This works fine in my localhost but in after uploading it to server it shows server time. How do I convert it to IST?

private void admin_Default2_Load(object sender, EventArgs e)
{
    if (DateTime.Now.Hour < 12) {
        lblGreeting.Text = "Good Morning";
        lblDate.Text = Convert.ToString(DateTime.Now);
    } else if (DateTime.Now.Hour < 17) {
        lblGreeting.Text = "Good Afternoon";
        lblDate.Text = Convert.ToString(DateTime.Now);
    } else {
        lblGreeting.Text = "Good Evening";
        lblDate.Text = Convert.ToString(DateTime.Now);
    }
}
SUN
  • 973
  • 14
  • 38
  • It would be *far* better if you did this on the browser with JavaScript. Don't *assume* what the timezone is – Panagiotis Kanavos Nov 11 '16 at 12:42
  • Check [Determine a User's Timezone](http://stackoverflow.com/questions/13/determine-a-users-timezone) - you can get the timezone with JavaScript or just ask the user. – Panagiotis Kanavos Nov 11 '16 at 12:45
  • By what logic are you assuming that IST is the right time zone for the user? Are your users limited to those inside India? – Matt Johnson-Pint Nov 11 '16 at 19:52
  • BTW - [in 2014, there was a second time zone created for part of India](http://www.timeanddate.com/news/time/india-assam-time-zone.html) - but it was never put into effect. Who knows if it will or will not again at some point in the future. So hardcoding UTC+5:30 could eventually become a problem. It is indeed [still being considered](http://www.ndtv.com/india-news/northeast-wants-its-own-time-zone-will-assams-new-government-help-1421106). – Matt Johnson-Pint Nov 11 '16 at 19:55

3 Answers3

1
DateTime serverTime = DateTime.Now; // gives you current Time in server timeZone
DateTime utcTime = serverTime.ToUniversalTime; // convert it to Utc using timezone setting of server computer
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzi); // convert from utc to local
if (localTime  < 12) {
        lblGreeting.Text = "Good Morning";
        lblDate.Text = Convert.ToString(localTime );
    } else if (localTime  < 17) {
        lblGreeting.Text = "Good Afternoon";
        lblDate.Text = Convert.ToString(localTime );
    } else {
        lblGreeting.Text = "Good Evening";
        lblDate.Text = Convert.ToString(localTime );
    }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • This will break if someone outside this timezone tries to use the site. Better to let the *user* decide this, as a setting, or send the UTC time to the browser and format it using Javascript – Panagiotis Kanavos Nov 11 '16 at 12:41
  • @PanagiotisKanavos OP has mentioned IST – Sajeetharan Nov 11 '16 at 12:42
  • Whoever donwvoted this, consider that storing `India Standard Time` in a user's settings is a fine way to specify the user's preferred timezone. – Panagiotis Kanavos Nov 11 '16 at 12:43
  • OP will realize the problem as soon as someone outside India tries to access the site. In any case, a simple `new Date()` on the page is good enough to generate a greeting – Panagiotis Kanavos Nov 11 '16 at 12:44
0

Just convert your current time in to UTC & add 5.5 hours if you want IST

if (DateTime.UtcNow.AddHours(5.5).Hour < 12) {
    lblGreeting.Text = "Good Morning";
    lblDate.Text = Convert.ToString(DateTime.UtcNow.AddHours(5.5));
} else if (DateTime.UtcNow.AddHours(5.5).Hour < 17) {
    lblGreeting.Text = "Good Afternoon";
    lblDate.Text = Convert.ToString(DateTime.UtcNow.AddHours(5.5));
} else {
    lblGreeting.Text = "Good Evening";
    lblDate.Text = Convert.ToString(DateTime.UtcNow.AddHours(5.5));
}
SuRaj Creator
  • 945
  • 1
  • 9
  • 25
  • Is this some standard wrong answer that everyone copies? The framework has some perfectly good libraries for switching timezones **assuming** you know the user's timezone. Storing the offset is *not* enough, due to changing DST rules (at least) – Panagiotis Kanavos Nov 11 '16 at 12:50
  • The offset for the IST timezone is as certain as the continued circulation of 1000 notes, and just as "difficult" to change with a new law. – Panagiotis Kanavos Nov 11 '16 at 12:53
-2

IST is +5:30 hours ahead Universal Time hence you can use the below code

       if (DateTime.Now.ToUniversalTime().AddHours(5).AddMinutes(30).Hour < 12)
        {
            lblGreeting.Text = "Good Morning";
            lblDate.Text = Convert.ToString(DateTime.Now);
        }
        else if (DateTime.Now.ToUniversalTime().AddHours(5).AddMinutes(30).Hour < 17)
        {
            lblGreeting.Text = "Good Afternoon";
            lblDate.Text = Convert.ToString(DateTime.Now);
        }
        else
        {
            lblGreeting.Text = "Good Evening";
            lblDate.Text = Convert.ToString(DateTime.Now);
        }
Ratna
  • 2,289
  • 3
  • 26
  • 50
  • Hardcoding a value like this is a **very** bad idea. This code will break in whenever daylight saving time changes (about 6 months?). – Panagiotis Kanavos Nov 11 '16 at 12:39
  • Not in this case because UniversalTime never follows Daylight saving, And IST is always +5:30 hour ahead. – Ratna Nov 11 '16 at 12:40
  • Really? Are you sure the law won't change next month? Next year? With a week's notice, as Venezuela did? Are you sure 1000 notes won't be withdrawn without notice? – Panagiotis Kanavos Nov 11 '16 at 12:47