I am writing a utility in which time is being displayed. The problem is, the time being displayed is for the timezone +1. I want to add functinallity so that the user can not only display this time, but also choose which timezone they would like it to be displayed in.

Say for example, the user is shown the time: 18:00:00 as the time from +1 zone.

Now if the user wanted it to be shown in -5 GMT, they could simply choose it from a dropdown box and show the time. The problem I'm facing is in the actual conversion. I dnt want to go and write a whole DateTime class of my own, but simply use the current class in c# and convert the time.

Any suggestions??

Recommended Answers

All 4 Replies

Take a look at the DateTimeOffset struct.
Also the TimeZone class

You should be able to use those and then use the DateTime AddHours method to get the new date you are looking for.

//Jerry

biggest problem you will have is when summertime kicks in (or daylight savings whatever its called where you are) as its not all done at the same time the world over.

The thing is, I already know the time I'm trying to convert is in +1 GMT Zone. So why not just convert it into UMT and then to the local time of the user. The problem would be knowing when +1 GMT is observing their Daylight Saving.

hi,

i had to do this quickly for a project.

the approach broadly is to write a function to work out whether a specific date was in summertime or not.

private string getSeason(DateTime date) 
		{
			//need to understand the logic
			//essentially the same rula as British Summertime
			//
			DateTime lastSundayInMarch = lastSundayOfTheMonth(3, DateTime.Today.Year);
			DateTime lastSundayInOctober = lastSundayOfTheMonth(10, DateTime.Today.Year);
			
			
			if (DateTime.Now.ToUniversalTime() >= lastSundayInMarch.ToUniversalTime() && DateTime.Now.ToUniversalTime() <= lastSundayInOctober.ToUniversalTime() ) 
			{
				return "s-" + System.Convert.ToString( DateTime.Today.Year);
			}
			else
			{
				return "w-" + System.Convert.ToString( DateTime.Today.Year+1);
			}
			
		}

		private DateTime lastSundayOfTheMonth(int month, int year)
		{
			month++;

			DateTime tempDate = new DateTime(year,month,1);		
			System.TimeSpan duration = new System.TimeSpan(1, 0, 0, 0);
			DateTime result = tempDate.Subtract(duration);

			while (String.Compare( String.Format("{0:ddd}",result),"Sun")!=0)
				result = result.Subtract(duration);
		
			return result;

		}

hth

tristian o'brien

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.