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. Convert Time

Convert Time

Scheduled Pinned Locked Moved C#
help
9 Posts 4 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.
  • O Offline
    O Offline
    onetreeup
    wrote on last edited by
    #1

    = ConvertTime(timeArray[x]); } for (int x = 0; x < timeArray.Length; x++) { Console.WriteLine(timeArray[x] + " " + decTimeArray[x]); } Console.ReadKey(); } } public static double ConvertTime(string rawTime) { int hours; int minutes; double dminutes; double decimalTime; string delimStr = ":"; char[] delimiter = delimStr.ToCharArray(); string[] split; split = rawTime.Split(delimiter); hours = Convert.ToInt32(split[0]); minutes = Convert.ToInt32(split[1]); if (minutes < 7) dminutes = 0.00; else if (minutes < 22) dminutes = 0.25; else if (minutes < 37) dminutes = 0.50; else if (minutes < 53) dminutes = 0.75; else dminutes = 1.0; return decimalTime = dminutes + hours;

    L L O 3 Replies Last reply
    0
    • O onetreeup

      = ConvertTime(timeArray[x]); } for (int x = 0; x < timeArray.Length; x++) { Console.WriteLine(timeArray[x] + " " + decTimeArray[x]); } Console.ReadKey(); } } public static double ConvertTime(string rawTime) { int hours; int minutes; double dminutes; double decimalTime; string delimStr = ":"; char[] delimiter = delimStr.ToCharArray(); string[] split; split = rawTime.Split(delimiter); hours = Convert.ToInt32(split[0]); minutes = Convert.ToInt32(split[1]); if (minutes < 7) dminutes = 0.00; else if (minutes < 22) dminutes = 0.25; else if (minutes < 37) dminutes = 0.50; else if (minutes < 53) dminutes = 0.75; else dminutes = 1.0; return decimalTime = dminutes + hours;

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

      Still at it? To get a time span expressed as a floating-point number of hours but rounded to a quarter, I would try something along these lines (code not tested):

      private double ToHoursButRoundedToQuarter(string s) {
      TimeSpan span=TimeSpan.Parse(s);
      int minutes=span.TotalMinutes+7;
      int quarters=minutes/15;
      return quarters*0.25;
      }

      :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      1 Reply Last reply
      0
      • O onetreeup

        = ConvertTime(timeArray[x]); } for (int x = 0; x < timeArray.Length; x++) { Console.WriteLine(timeArray[x] + " " + decTimeArray[x]); } Console.ReadKey(); } } public static double ConvertTime(string rawTime) { int hours; int minutes; double dminutes; double decimalTime; string delimStr = ":"; char[] delimiter = delimStr.ToCharArray(); string[] split; split = rawTime.Split(delimiter); hours = Convert.ToInt32(split[0]); minutes = Convert.ToInt32(split[1]); if (minutes < 7) dminutes = 0.00; else if (minutes < 22) dminutes = 0.25; else if (minutes < 37) dminutes = 0.50; else if (minutes < 53) dminutes = 0.75; else dminutes = 1.0; return decimalTime = dminutes + hours;

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Your code makes very little sense to me. Why not use DateTime and/or TimeSpan?

        1 Reply Last reply
        0
        • O onetreeup

          = ConvertTime(timeArray[x]); } for (int x = 0; x < timeArray.Length; x++) { Console.WriteLine(timeArray[x] + " " + decTimeArray[x]); } Console.ReadKey(); } } public static double ConvertTime(string rawTime) { int hours; int minutes; double dminutes; double decimalTime; string delimStr = ":"; char[] delimiter = delimStr.ToCharArray(); string[] split; split = rawTime.Split(delimiter); hours = Convert.ToInt32(split[0]); minutes = Convert.ToInt32(split[1]); if (minutes < 7) dminutes = 0.00; else if (minutes < 22) dminutes = 0.25; else if (minutes < 37) dminutes = 0.50; else if (minutes < 53) dminutes = 0.75; else dminutes = 1.0; return decimalTime = dminutes + hours;

          O Offline
          O Offline
          onetreeup
          wrote on last edited by
          #4

          This was working, but right now I am getting the following error: Expected class, delegate,enum, interface, or struct I am new to c#, and in a class where we are not using the timespan yet, and will not touch on that in this semester. I you could look at the code I have supplied, and maybe help me find the error, that would be great. Thanks :-D

          L A 2 Replies Last reply
          0
          • O onetreeup

            This was working, but right now I am getting the following error: Expected class, delegate,enum, interface, or struct I am new to c#, and in a class where we are not using the timespan yet, and will not touch on that in this semester. I you could look at the code I have supplied, and maybe help me find the error, that would be great. Thanks :-D

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Where exactly?

            L 1 Reply Last reply
            0
            • L Lost User

              Where exactly?

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

              that's cheating. The challenge is to locate the error (and to get someone else to that for you). :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              L 1 Reply Last reply
              0
              • O onetreeup

                This was working, but right now I am getting the following error: Expected class, delegate,enum, interface, or struct I am new to c#, and in a class where we are not using the timespan yet, and will not touch on that in this semester. I you could look at the code I have supplied, and maybe help me find the error, that would be great. Thanks :-D

                A Offline
                A Offline
                agent00zelda
                wrote on last edited by
                #7

                From the code you posted and the error you say you are getting, I think your brackets are off. You should have { } wrapped around your class methods (Main and ConvertTime) and also to enclose them.

                O 1 Reply Last reply
                0
                • L Luc Pattyn

                  that's cheating. The challenge is to locate the error (and to get someone else to that for you). :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Lol :laugh:

                  1 Reply Last reply
                  0
                  • A agent00zelda

                    From the code you posted and the error you say you are getting, I think your brackets are off. You should have { } wrapped around your class methods (Main and ConvertTime) and also to enclose them.

                    O Offline
                    O Offline
                    onetreeup
                    wrote on last edited by
                    #9

                    Thanks, that's what I needed, another pair of eyes. I must have deleted a bracket accidentally. Runs fine now.

                    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