How does Now() work for different time zone?

VAer-4038 766 Reputation points
2021-01-02T09:01:57.5+00:00

The application is for all the employees, who work in different time zone. There is an access table to record employee's log in time. It just use the code Now()

For example, John works in NY and he logs in at 9pm eastern time ; Jennifer works in Los Angeles and she logs in the application about the same time as John. But it is 6pm in Los Angeles.

So in access table, will Jennifer's login time is 6pm and John's login time is 9pm.

Their login time is about the same, is it possible to consider time zone when recording login time? John's login time should be close to Jennifer's, not three hours apart.

Thanks.

Edit: Let me change the question a little bit(after receiving some questions), Jennifer clicks the login button at 6pm Los Angeles local time, and I would like to record her login Date/Time as 9pm Eastern Time US. So it does not matter where an employee works, as long as he/she log in the application, record the date/time and convert it to US Eastern Time, then store it Access table. How should I do it?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,203 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dylan Zhu-MSFT 6,421 Reputation points
    2021-01-04T03:53:02.887+00:00

    Hi VAer,

    >record the date/time and convert it to US Eastern Time, then store it Access table.

    According to RLWA32's reply, you can refer to this sample:

            public void InsertRecord()  
            {  
                string connString = @"...";  
                using (OleDbConnection connection = new OleDbConnection(connString))  
                {  
                    //from RLWA32  
                    DateTime dtLocal = DateTime.Now;  
                    TimeZoneInfo tziEST = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");  
                    DateTime dtEastern = TimeZoneInfo.ConvertTime(dtLocal, tziEST);  
    
                    OleDbCommand cmd = new OleDbCommand("INSERT into [Table](Employee, LoginTime) Values(@Name, @Time)");  
                    cmd.Connection = connection;  
                    connection.Open();  
                    cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = "Jennifer";  
                    cmd.Parameters.Add("@Time", OleDbType.VarChar).Value = dtEastern.ToString();  
    
                    cmd.ExecuteNonQuery();  
    
                    MessageBox.Show("ok");  
                }  
            }  
    

    The document about access command: Insert, update, and delete records from a table using Access SQL

    Best Regards, Dylan

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our * *documentation* to enable e-mail notifications if you want to receive the related email notification for this thread.**


3 additional answers

Sort by: Most helpful
  1. Jaliya Udagedara 2,821 Reputation points MVP
    2021-01-02T09:08:45.597+00:00

    If you want to store the Offset along with the DateTime, you need to use DateTimeOffset. The most important difference between DateTime and DateTimeOffset is DateTime does not store timezone information, but DateTimeOffset does that.

    For example,

    Console.WriteLine(DateTime.Now);  
    Console.WriteLine(DateTimeOffset.Now);  
    

    This would output,

    1/2/2021 10:10:32 PM  
    1/2/2021 10:10:32 PM +13:00  
    

    +13:00, as I am in New Zealand.


  2. RLWA32 45,701 Reputation points
    2021-01-02T09:25:43.743+00:00

    Another possibility is for managed code to use DateTime.UtcNow to capture time data. The recorded UTC time could be converted to a local time for display. The advantage of recording the UTC time is that the offset calculated by DateTimeOffset is not fullly time zone aware - As per system.datetimeoffset -

    "Although a DateTimeOffset value includes an offset, it is not a fully time zone-aware data structure. While an offset from UTC is one characteristic of a time zone, it does not unambiguously identify a time zone. Not only do multiple time zones share the same offset from UTC, but the offset of a single time zone changes if it observes daylight saving time. This means that, as soon as a DateTimeOffset value is disassociated from its time zone, it can no longer be unambiguously linked back to its original time zone."

    I'm not an Access user so I have no idea if the above is relevant to that environment.

    0 comments No comments

  3. RLWA32 45,701 Reputation points
    2021-01-02T23:23:20.57+00:00

    For example, to convert the local time to Eastern time -

        DateTime dtLocal = DateTime.Now;
        TimeZoneInfo tziEST = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        DateTime dtEastern = TimeZoneInfo.ConvertTime(dtLocal, tziEST);
        Console.WriteLine("Local : {0} {1}\nEastern : {2} {3}",
            dtLocal,
            TimeZoneInfo.Local.IsDaylightSavingTime(dtLocal) ? TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName,
            dtEastern,
            tziEST.IsDaylightSavingTime(dtEastern) ? tziEST.DaylightName : tziEST.StandardName);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.