Home
Manage Your Code
Snippet: UTC Time Checking (C#)
Title: UTC Time Checking Language: C#
Description: Convert from local time using offset and see if a given time is between two others. Views: 402
Author: Matt Dinovo Date Added: 12/13/2006
Copy Code  
1double startHour = 8; //Starting hour (e.g. 08:00)
2double interval = 23; //Ending hour (e.g. 23:00)
3double offset = -240 / 60; //Offset In Hours
4
5//This is how we calculate
6DateTime localStart = DateTime.Parse("00:00").AddHours(startHour);
7DateTime utcStart = localStart.AddHours(Math.Abs(offset));
8
9System.Diagnostics.Debug.WriteLine("Start: {0} ({1})", utcStart, utcStart.AddHours(offset) );
10
11DateTime utcNow = DateTime.UtcNow;
12System.Diagnostics.Debug.WriteLine("Now: {0} ({1})", utcNow, utcNow.AddHours(offset));
13
14DateTime utcEnd = utcStart.AddHours(interval);
15System.Diagnostics.Debug.WriteLine("End: {0} ({1})", utcEnd, utcEnd.AddHours(offset));
16
17if(utcNow>=utcStart && utcNow<=utcEnd)
18{
19	System.Diagnostics.Debug.WriteLine("Passed");
20}
21else
22{
23	System.Diagnostics.Debug.WriteLine("Failed");
24}