Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. What we can do with date and time offset data

What we can do with date and time offset data

Scheduled Pinned Locked Moved ASP.NET
questioncomlounge
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    see here we store offset. so tell me what is the importance of storing offset data along with date and time ? what we can do with offset data if we store ? public void DemoDateTimeOffset() { DateTimeOffset offsetValue = new DateTime(2017, 1, 21, 4, 34, 0); using (SqlConnection cn = new SqlConnection(myConnection)) { string commandText = @" INSERT INTO DateTimeDemo (DateTimeExample,DateTimeOffsetExample) VALUES (@DateTimeExample,@DateTimeOffsetExample)"; using (SqlCommand cmd = new SqlCommand(commandText, cn)) { cmd.Parameters.AddWithValue("@DateTimeExample", DateTime.Now); cmd.Parameters.AddWithValue("@DateTimeOffsetExample", offsetValue); cn.Open(); cmd.ExecuteNonQuery(); } } } [screen shot] thanks

    tbhattacharjee

    J 1 Reply Last reply
    0
    • T Tridip Bhattacharjee

      see here we store offset. so tell me what is the importance of storing offset data along with date and time ? what we can do with offset data if we store ? public void DemoDateTimeOffset() { DateTimeOffset offsetValue = new DateTime(2017, 1, 21, 4, 34, 0); using (SqlConnection cn = new SqlConnection(myConnection)) { string commandText = @" INSERT INTO DateTimeDemo (DateTimeExample,DateTimeOffsetExample) VALUES (@DateTimeExample,@DateTimeOffsetExample)"; using (SqlCommand cmd = new SqlCommand(commandText, cn)) { cmd.Parameters.AddWithValue("@DateTimeExample", DateTime.Now); cmd.Parameters.AddWithValue("@DateTimeOffsetExample", offsetValue); cn.Open(); cmd.ExecuteNonQuery(); } } } [screen shot] thanks

      tbhattacharjee

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      The offset is the difference between UTC time and local time. To know why it is stored and for what purpose ask the creator of the database. DateTime stamps should be always stored in UTC. When displaying such values retrieved from a database, they may be converted to local time using the user/system settings. The only reasons to store the offset might be to show the stamp using the local time of the system that has written the data (not very meaningful) or knowing the time zone where the creator resides. In your example code the value it is not an offset but a fixed stamp. The real offset would be the difference between the values. So I would name that value LocalTime instead of DateTimeOffset.

      T 1 Reply Last reply
      0
      • J Jochen Arndt

        The offset is the difference between UTC time and local time. To know why it is stored and for what purpose ask the creator of the database. DateTime stamps should be always stored in UTC. When displaying such values retrieved from a database, they may be converted to local time using the user/system settings. The only reasons to store the offset might be to show the stamp using the local time of the system that has written the data (not very meaningful) or knowing the time zone where the creator resides. In your example code the value it is not an offset but a fixed stamp. The real offset would be the difference between the values. So I would name that value LocalTime instead of DateTimeOffset.

        T Offline
        T Offline
        Tridip Bhattacharjee
        wrote on last edited by
        #3

        thanks for reply. 1) would you post some code to grab the offset between local date and time and UTC date and time. 2) would show how to convert utc date and time to local one if we know the offset ? please share the knowledge if possible. thanks

        tbhattacharjee

        J 1 Reply Last reply
        0
        • T Tridip Bhattacharjee

          thanks for reply. 1) would you post some code to grab the offset between local date and time and UTC date and time. 2) would show how to convert utc date and time to local one if we know the offset ? please share the knowledge if possible. thanks

          tbhattacharjee

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          It depends on the used language and the used DateTime format. If you have a format that uses the elapsed seconds or milliseconds since some epoch, just do addition / subtraction. If you have a DateTime class, use the provided class functions to add/subtract time spans and get the difference between two objects. These classes have usually also functions to convert between UTC and local time using the user or system settings for the local time zone. For JavaScript see for example Date - JavaScript | MDN[^]. For .NET see DateTime Structure (System)[^]. There is usually no need to get the offset. Use conversion functions instead. The only situation to use an offset might be when it is configured by a user setting on the server (e.g. the user has specified his time zone). Then use the language dependant addition method with a time span set to the offset. With .NET DateTime, use for example DateTime.ToUniversalTime Method (System)[^] and DateTime.ToLocalTime Method (System)[^]. But note that the object must be in the source format (when using ToLocalTime for example, the object must contain a UTC time).

          T 1 Reply Last reply
          0
          • J Jochen Arndt

            It depends on the used language and the used DateTime format. If you have a format that uses the elapsed seconds or milliseconds since some epoch, just do addition / subtraction. If you have a DateTime class, use the provided class functions to add/subtract time spans and get the difference between two objects. These classes have usually also functions to convert between UTC and local time using the user or system settings for the local time zone. For JavaScript see for example Date - JavaScript | MDN[^]. For .NET see DateTime Structure (System)[^]. There is usually no need to get the offset. Use conversion functions instead. The only situation to use an offset might be when it is configured by a user setting on the server (e.g. the user has specified his time zone). Then use the language dependant addition method with a time span set to the offset. With .NET DateTime, use for example DateTime.ToUniversalTime Method (System)[^] and DateTime.ToLocalTime Method (System)[^]. But note that the object must be in the source format (when using ToLocalTime for example, the object must contain a UTC time).

            T Offline
            T Offline
            Tridip Bhattacharjee
            wrote on last edited by
            #5

            i guess if i store date time in utc format and if i store also offset then i can convert utc date time to local one just adding or subtract offset hours from UTC date and time. am i right ?

            tbhattacharjee

            J 1 Reply Last reply
            0
            • T Tridip Bhattacharjee

              i guess if i store date time in utc format and if i store also offset then i can convert utc date time to local one just adding or subtract offset hours from UTC date and time. am i right ?

              tbhattacharjee

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              Yes, you are right. But storing is only necessary if you want to track this kind of information (the local time of the system that has created the time stamp). This is often not necessary because you usually show local times using the time zone of the system displaying the time. If for example a time stamp is created by a user in India and another user somwhere in the U.S. is viewing the related data, it makes more sense to show it in the local time of the viewer or even UTC rather than using the Indian time.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups