How to get the right timezone (server timezone) from the UTC date format?
As follow up of the below link.
How to format this date type 2010-06-24T00:00:00Z to sun,24/06/10 7.15 p.m (CDT) using javascript or jquery
I'm convert开发者_JS百科ing the utc date format to simple date format:
2010-06-24T00:00:00Z to sun,24/06/10 7.15 p.m (CDT) (converted time)
But is there a way to identify the valid timezone, as the above conversion always detects the time zone based on client machine. Or is it that we can't detect the valid time zone from UTC?
UTC has no time zone, it is "universal" because it is always the same no matter where you are. Time zone information has to be determined by the observer.
You didn't define what you mean by "valid timezone" -- do you mean local timezone? (UTC is a perfectly valid timezone, after all--or at least it is if you're in the Western European Time Zone and it's not British Summer Time…)
If you have a UTC date and you want to turn it into a date in the local timezone, that's straightforward:
function UTCtoLocal(inDate) {
// inDate can be null, a string or an object
// if it's an object, it can be a date or event
var localDate = new Date();
var utcDate = new Date().toUTCString();
if (inDate) {
if (typeof inDate == "string") {
utcDate = inDate;
}
else {
if (inDate.getDay) { // is it really a date?
utcDate = inDate.toString();
}
}
}
utcDate = utcDate.substr(0, utcDate.length-3);
localDate.setTime(Date.parse(utcDate));
return localDate;
}
You can use below code in C# to get the date by passing the timezone value (e.g.: +08:00:00)
Console.WriteLine("Enter the Time zone: ");
string zoneInfo = Console.ReadLine();
int Flags = 0;
ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
string myTime = "";
string displayName = "";
foreach (var timeZoneInfo in zones)
{
if (timeZoneInfo.BaseUtcOffset`enter code here`.ToString().Equals(zoneInfo))
{
string timeZoneId = timeZoneInfo.Id;`enter code here`
myTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId)).ToString();
displayName = timeZoneInfo.DaylightName.ToString();
break;
}
}
Console.WriteLine(displayName + ":" + myTime);
Console.ReadLine();
精彩评论