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. General Programming
  3. C#
  4. Time Subtract

Time Subtract

Scheduled Pinned Locked Moved C#
tutorial
6 Posts 6 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.
  • M Offline
    M Offline
    M Riaz Bashir
    wrote on last edited by
    #1

    Hi Respected Seniors, Kindly let me know one or two examples, How may I subtract / "Diffrence" time. for example: string strStudentTime = "10:00"; string strSystemTime = "10:15"; I need difference. Thank you (Riaz Bashir)

    H P L D J 5 Replies Last reply
    0
    • M M Riaz Bashir

      Hi Respected Seniors, Kindly let me know one or two examples, How may I subtract / "Diffrence" time. for example: string strStudentTime = "10:00"; string strSystemTime = "10:15"; I need difference. Thank you (Riaz Bashir)

      H Offline
      H Offline
      Hiren solanki
      wrote on last edited by
      #2

      You can use Timespan[^] when need any kind of difference between two dates or time.

      Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".

      1 Reply Last reply
      0
      • M M Riaz Bashir

        Hi Respected Seniors, Kindly let me know one or two examples, How may I subtract / "Diffrence" time. for example: string strStudentTime = "10:00"; string strSystemTime = "10:15"; I need difference. Thank you (Riaz Bashir)

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        As Hiren said, look at the TimeSpan class. Specifically, you'll need to read these values in (using TryParse) to a TimeSpan, and then you can get the difference from there.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        1 Reply Last reply
        0
        • M M Riaz Bashir

          Hi Respected Seniors, Kindly let me know one or two examples, How may I subtract / "Diffrence" time. for example: string strStudentTime = "10:00"; string strSystemTime = "10:15"; I need difference. Thank you (Riaz Bashir)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You should not hold times, dates, or datetimes in strings; strings here are useful only for showing actual values to human users. Keep them in DateTime types instead, so you have all DateTime operators and methods available, including subtraction, which results in a TimeSpan. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          1 Reply Last reply
          0
          • M M Riaz Bashir

            Hi Respected Seniors, Kindly let me know one or two examples, How may I subtract / "Diffrence" time. for example: string strStudentTime = "10:00"; string strSystemTime = "10:15"; I need difference. Thank you (Riaz Bashir)

            D Offline
            D Offline
            dasblinkenlight
            wrote on last edited by
            #5

            If it is a 24-hour clock, and the format is fixed as hh:mm, you can do it in a very simple way, without the system classes:

            private static int ParseTime(string s) {
            return 60*int.Parse(s.Substring(0, 2)) + int.Parse(s.Substring(3));
            }
            ...
            var diffInMinutes = ParseTime(strSystemTime) - ParseTime(strSystemTime);

            If your code needs to be more robust than that, you should use DateTime to parse your strings, and then use the "-" operator to obtain their difference.

            1 Reply Last reply
            0
            • M M Riaz Bashir

              Hi Respected Seniors, Kindly let me know one or two examples, How may I subtract / "Diffrence" time. for example: string strStudentTime = "10:00"; string strSystemTime = "10:15"; I need difference. Thank you (Riaz Bashir)

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              If you have a time then you should use TimeSpan. If you have a timestamp then you should use DateTimeOffset (rather than DateTime.)

              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