Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Timezone

Status
Not open for further replies.

blar9

Programmer
Mar 12, 2007
39
US
I have a website that shows time information that needs to be based off the users timezone not the servers. I can convert server time to utc. I need a function that will allow me to send in servers time and pacific or eastern and then return the conversion. I am not able to find something that works like this any suggestions.?

THANKS
 
Here is my TimeZone Conversion code. I am only dealing with the 3 main US time zones

SourceTimeZone is the time zone that the datetime from originated from, the ClientTimeZone is the timezone I need to conver to.

Code:
static internal string ConvertToLocalTime(string SourceTimeZone, TimeZone ClientTimeZone, DateTime DateTimeValue)
		{
			string sLocalTime = Convert.ToString(DateTimeValue);
			switch (SourceTimeZone.ToUpper()[0])
			{
				case 'E':
				switch (ClientTimeZone.StandardName[0])
				{
					case 'E':
						sLocalTime = sLocalTime;
						break;
					case 'C':
						sLocalTime = DateTimeValue.AddHours(-1).ToString();
						break;
					case 'M':
						sLocalTime = DateTimeValue.AddHours(-2).ToString();
						break;
					case 'P':
						sLocalTime = DateTimeValue.AddHours(-3).ToString();
						break;
				}
					break;
				case 'C':
				switch (ClientTimeZone.StandardName[0])
				{
					case 'E':
						sLocalTime = DateTimeValue.AddHours(+1).ToString();
						break;
					case 'C':
						sLocalTime = sLocalTime;
						break;
					case 'M':
						sLocalTime = DateTimeValue.AddHours(-1).ToString();
						break;
					case 'P':
						sLocalTime = DateTimeValue.AddHours(-2).ToString();
						break;
				}
					break;
				case 'M':
				switch (ClientTimeZone.StandardName[0])
				{
					case 'E':
						sLocalTime = DateTimeValue.AddHours(+2).ToString();
						break;
					case 'C':
						sLocalTime = DateTimeValue.AddHours(+1).ToString();
						break;
					case 'M':
						sLocalTime = sLocalTime;
						break;
					case 'P':
						sLocalTime = DateTimeValue.AddHours(-1).ToString();
						break;
				}
					break;
				case 'P':
				switch (ClientTimeZone.StandardName[0])
				{
					case 'E':
						sLocalTime = DateTimeValue.AddHours(+3).ToString();
						break;
					case 'C':
						sLocalTime = DateTimeValue.AddHours(+2).ToString();
						break;
					case 'M':
						sLocalTime = DateTimeValue.AddHours(+1).ToString();
						break;
					case 'P':
						sLocalTime = sLocalTime;
						break;
				}
					break;
			}
			return sLocalTime;
			}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top