我有以下代码,根据当前本地时间向用户显示Wishing消息。这在我的localhost中工作正常,但在上传到服务器后它显示服务器时间。如何将其转换为IST?
children
答案 0 :(得分: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 );
}
答案 1 :(得分:0)
只需将您当前的时间转换为UTC&amp;如果你想要IST,可以增加5.5小时
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));
}
答案 2 :(得分:-2)
IST比世界时间早5:30小时,因此您可以使用以下代码
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);
}