Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 DateTimeOffset serverTime = DateTimeOffset.Now;

               DateTimeOffset localTime = TimeZoneInfo.ConvertTime(serverTime, TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time"));


if (localTime.Date >= eddate.Date && localTime.Date <= stdate.Date)
               {
                   if (localTime >= sttime && localTime <= edtime)
                   {

                       MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                       MySqlCommand MyCommand2 = new MySqlCommand(query, MyConn2);
                       MySqlDataReader MyReader2;
                       MyConn2.Open();
                       MyReader2 = MyCommand2.ExecuteReader();
                       Label2.Text = "Thank you for You Vote";
                       //  System.Diagnostics.Debug.WriteLine();

                   }
                   else
                   {
                       Label2.Text = "Please check the eletion time!";
                       Label2.ForeColor = System.Drawing.Color.Yellow;
                   }
               }
               else
               {
                   Label2.Text = "Please check the eletion date !";
                   Label2.ForeColor = System.Drawing.Color.Purple;
               }


What I have tried:

I'm trying to get the date in the timezone that I specified(Singapore Standard Time) and compare it with the date I displayed in gridview. I'm able to use the Singapore Standard Time when validating the time. However, for the date, It still not able to get the date in Singapore Standard Time.
Posted
Updated 12-Nov-20 10:29am
v5

Singapore and China are both in the same time zone. +8 GMT
 
Share this answer
 
Comments
ycwong99 12-Nov-20 13:14pm    
Hi, I'm actually made a mistake. I'm aware that they are using the same timezone, I'd like to get the current date in the Singapore Standard timezone.
[no name] 12-Nov-20 13:40pm    
"Local time" is the server time. It's not the same as "client (local) computer time" if you're accessing from another time zone. The server needs the client's offset or time to do what you seem to be implying. Windows clients sync their clocks based on location; cross a time zone and they'll flip, sending field personnel into confusion when they forget that point.
ycwong99 12-Nov-20 13:49pm    
Hi, I had updated my code using the offset. How can I get the date of the Singapore Standard Time?
Your if-condition seems to be off. Try changing it to this:

C#
if (localTime.Date >= stdate.Date && localTime.Date <= eddate.Date){
  //Do stuff here
}


What has changed in the preceding code is switched the enddate and eddate for comparing.
 
Share this answer
 
v2
You shouldn't be screwing around with time zones.

If you're going to have a server in one time zone and a client in another, ALL datetimes should be converted to UTC before being sent between client and server. ALL datetimes should be stored in your database in UTC.

When data is displayed, that's when you convert from UTC to the local time zone. When data is entered, you convert the input datetime to UTC before sending it anywhere.

This way, every machine takes using the "universal" time zone.
 
Share this answer
 
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900