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 conversion

Time conversion

Scheduled Pinned Locked Moved C#
help
17 Posts 4 Posters 3 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.
  • L Luc Pattyn

    Hi, a DateTime always contains date and time. TimeOfDay gives time since midnight. Hence:

    DateTime dt=DateTime.Parse(myInputString);
    TimeSpan time=dt.TimeOfDay;

    :)

    Luc Pattyn [My Articles]

    A Offline
    A Offline
    Anka_Ame
    wrote on last edited by
    #3

    But if my string is like this "07:30", what can I do? Because I tried that method and my result is 00:00:00. :doh:

    L 1 Reply Last reply
    0
    • A Anka_Ame

      Hi, I have a problem with the conversion of a string of type "07:30" into a time. I want to take only the time, without the date. I tried DateTime openTime = DateTime.Now.ToShortTimeString(open); ,where open = "07:30". Please help me with this problem.

      M Offline
      M Offline
      Malcolm Smart
      wrote on last edited by
      #4

      try //get the current date and time System.DateTime nowTime = System.DateTime.Now; //set our time System.DateTime openTime = System.DateTime.Parse(nowTime .ToShortDateString() + " 07:30:00"); //get difference TimeSpan open = nowTime - openTime; if (nowTime > openTime) //we're open Console.WriteLine("We've been open for {0:0} minutes", open.TotalMinutes); else Console.WriteLine("We will open in {0:0} minutes", -open.TotalMinutes); There must be better methods though...?

      Regards Angel *********************************************

      A 1 Reply Last reply
      0
      • M Malcolm Smart

        try //get the current date and time System.DateTime nowTime = System.DateTime.Now; //set our time System.DateTime openTime = System.DateTime.Parse(nowTime .ToShortDateString() + " 07:30:00"); //get difference TimeSpan open = nowTime - openTime; if (nowTime > openTime) //we're open Console.WriteLine("We've been open for {0:0} minutes", open.TotalMinutes); else Console.WriteLine("We will open in {0:0} minutes", -open.TotalMinutes); There must be better methods though...?

        Regards Angel *********************************************

        A Offline
        A Offline
        Anka_Ame
        wrote on last edited by
        #5

        The problem is that I will take the string from a Xml file and I cant use it like that. I really need help.

        M 1 Reply Last reply
        0
        • A Anka_Ame

          The problem is that I will take the string from a Xml file and I cant use it like that. I really need help.

          M Offline
          M Offline
          Malcolm Smart
          wrote on last edited by
          #6

          what exactly are you trying to do? can you post more code?

          Regards Angel *********************************************

          A 1 Reply Last reply
          0
          • A Anka_Ame

            But if my string is like this "07:30", what can I do? Because I tried that method and my result is 00:00:00. :doh:

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

            When I run the code snippet

            DateTime dt=DateTime.Parse("07:30");
            TimeSpan time=dt.TimeOfDay;
            log(time.ToString());
            

            I get "07:30:00" as expected. BTW if you're not satisfied by Parse, there also is DateTime.ParseExact(). :)

            Luc Pattyn [My Articles]

            M 1 Reply Last reply
            0
            • M Malcolm Smart

              what exactly are you trying to do? can you post more code?

              Regards Angel *********************************************

              A Offline
              A Offline
              Anka_Ame
              wrote on last edited by
              #8

              I read a Xml file and from it I will have a string, for example the string name is open="07:30". I tried to read it like this DateTime oTime = DateTime.Parse(open); TimeSpan openTime = oTime.TimeOfDay; where openTime is of type TimeSpan. But the result is 00:00:00 and it doesnt work.

              M 1 Reply Last reply
              0
              • A Anka_Ame

                I read a Xml file and from it I will have a string, for example the string name is open="07:30". I tried to read it like this DateTime oTime = DateTime.Parse(open); TimeSpan openTime = oTime.TimeOfDay; where openTime is of type TimeSpan. But the result is 00:00:00 and it doesnt work.

                M Offline
                M Offline
                Malcolm Smart
                wrote on last edited by
                #9

                You still haven't posted any code.... I hope this isn't homework I am helping with...... So you have xml file eg:- <myxml> <times open="07:30" close="09:30" /> </myxml> And you want to know how long until it opens. Timespan is the difference between two times. If you want to know how long it is until the shop opens, then you need two things. The current time, and the time it opens. I am assuming you now have "07:30" stored in a string. I am also assuming that you want to know this information about 'today', and not any day in the future. C# DateTime class, as has already been mentioned, must have both a date and time portion. To get the current time :- System.DateTime currentTime = System.DateTime.Now; As I am writing this, this will give you: 02/05/2007 15:26:34 OK. That is the time now. You now want to know the time (today) when your shop/office etc opens. So, take the current datetime (currentTime) and replace the time portion with your time. System.DateTime openTime = System.DateTime.Parse(currentTime.ToShortDateString() + open + ":00" ); The extra ":00" is because a valid time (I think) needs hours:minutes:seconds and yours ("07:00") doesn't have this. So you now have currentTime and openTime. If you get the timespan of these, and use the TotalMinutes member, you get the number of minutes until the shop opens, or the number of minutes it has been open if this number is negative. TimeSpan open = nowTime - openTime; int MinutesUntilWeOpen = open.TotalMinutes; Is this homework or a real requirement? Post what you have tried up until now - it makes understanding your requirement a lot easier.

                Regards Angel *********************************************

                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  When I run the code snippet

                  DateTime dt=DateTime.Parse("07:30");
                  TimeSpan time=dt.TimeOfDay;
                  log(time.ToString());
                  

                  I get "07:30:00" as expected. BTW if you're not satisfied by Parse, there also is DateTime.ParseExact(). :)

                  Luc Pattyn [My Articles]

                  M Offline
                  M Offline
                  Malcolm Smart
                  wrote on last edited by
                  #10

                  Cool. I've always padded it out with current day/month/year. Learn something new everyday here....:)

                  Regards Angel *********************************************

                  1 Reply Last reply
                  0
                  • M Malcolm Smart

                    You still haven't posted any code.... I hope this isn't homework I am helping with...... So you have xml file eg:- <myxml> <times open="07:30" close="09:30" /> </myxml> And you want to know how long until it opens. Timespan is the difference between two times. If you want to know how long it is until the shop opens, then you need two things. The current time, and the time it opens. I am assuming you now have "07:30" stored in a string. I am also assuming that you want to know this information about 'today', and not any day in the future. C# DateTime class, as has already been mentioned, must have both a date and time portion. To get the current time :- System.DateTime currentTime = System.DateTime.Now; As I am writing this, this will give you: 02/05/2007 15:26:34 OK. That is the time now. You now want to know the time (today) when your shop/office etc opens. So, take the current datetime (currentTime) and replace the time portion with your time. System.DateTime openTime = System.DateTime.Parse(currentTime.ToShortDateString() + open + ":00" ); The extra ":00" is because a valid time (I think) needs hours:minutes:seconds and yours ("07:00") doesn't have this. So you now have currentTime and openTime. If you get the timespan of these, and use the TotalMinutes member, you get the number of minutes until the shop opens, or the number of minutes it has been open if this number is negative. TimeSpan open = nowTime - openTime; int MinutesUntilWeOpen = open.TotalMinutes; Is this homework or a real requirement? Post what you have tried up until now - it makes understanding your requirement a lot easier.

                    Regards Angel *********************************************

                    A Offline
                    A Offline
                    Anka_Ame
                    wrote on last edited by
                    #11

                    Thanks for trying to help me, but I dont want to make that. I want only to show the hour I read it from the Xml file. That's the problem, because I tried it and it shows something like 00:00, and not 07:30, like it should.

                    M 1 Reply Last reply
                    0
                    • A Anka_Ame

                      Thanks for trying to help me, but I dont want to make that. I want only to show the hour I read it from the Xml file. That's the problem, because I tried it and it shows something like 00:00, and not 07:30, like it should.

                      M Offline
                      M Offline
                      Malcolm Smart
                      wrote on last edited by
                      #12

                      OK - lets try again. Show me the whole function that you have at the moment. Show me your input, and what you expect as output.

                      Regards Angel *********************************************

                      A 1 Reply Last reply
                      0
                      • M Malcolm Smart

                        OK - lets try again. Show me the whole function that you have at the moment. Show me your input, and what you expect as output.

                        Regards Angel *********************************************

                        A Offline
                        A Offline
                        Anka_Ame
                        wrote on last edited by
                        #13

                        this is the function which reads the Xml File and is part of a class Cities. public class Cities : List { public void OpenCityList(string filename) { DataSet cityDS = new DataSet(); try { this.Clear(); cityDS.ReadXml(filename); DataRowCollection cities = cityDS.Tables[0].Rows; foreach (DataRow city in cities) { this.Add(new City(Convert.ToInt32(city["X"], CultureInfo.CurrentCulture), Convert.ToInt32(city["Y"], CultureInfo.CurrentCulture), Convert.ToInt32(city["Demand"], CultureInfo.CurrentCulture), Convert.ToString(city["Open"], CultureInfo.CurrentCulture))); } } finally { cityDS.Dispose(); } } private TimeSpan startHour; public TimeSpan StartHour { set { startHour = value; } get { return startHour; } } public void ShowCityHour() { foreach (City cityhour in this) { startHour = cityhour.OpenTime; } } } And in another class, named City I have : public class City { private int demand; private TimeSpan openTime; public City(int x, int y, int citydemand, string open)// I am interested about the string open { Location = new Point(x, y); demand = citydemand; DateTime oTime = DateTime.Parse(open); TimeSpan openTime = oTime.TimeOfDay; } public TimeSpan OpenTime { get { return openTime; } set { openTime = value; } } } It is something like that. I take the data from the Xml file and I store it in the string open, and I want to convert it to time, because after that I will have to sort the whole list in order of the hours.

                        L 1 Reply Last reply
                        0
                        • A Anka_Ame

                          this is the function which reads the Xml File and is part of a class Cities. public class Cities : List { public void OpenCityList(string filename) { DataSet cityDS = new DataSet(); try { this.Clear(); cityDS.ReadXml(filename); DataRowCollection cities = cityDS.Tables[0].Rows; foreach (DataRow city in cities) { this.Add(new City(Convert.ToInt32(city["X"], CultureInfo.CurrentCulture), Convert.ToInt32(city["Y"], CultureInfo.CurrentCulture), Convert.ToInt32(city["Demand"], CultureInfo.CurrentCulture), Convert.ToString(city["Open"], CultureInfo.CurrentCulture))); } } finally { cityDS.Dispose(); } } private TimeSpan startHour; public TimeSpan StartHour { set { startHour = value; } get { return startHour; } } public void ShowCityHour() { foreach (City cityhour in this) { startHour = cityhour.OpenTime; } } } And in another class, named City I have : public class City { private int demand; private TimeSpan openTime; public City(int x, int y, int citydemand, string open)// I am interested about the string open { Location = new Point(x, y); demand = citydemand; DateTime oTime = DateTime.Parse(open); TimeSpan openTime = oTime.TimeOfDay; } public TimeSpan OpenTime { get { return openTime; } set { openTime = value; } } } It is something like that. I take the data from the Xml file and I store it in the string open, and I want to convert it to time, because after that I will have to sort the whole list in order of the hours.

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

                          Anka_Ame wrote:

                          foreach (City cityhour in this) { startHour = cityhour.OpenTime; }

                          What does the variable startHour (and the property StartHour) mean in a list of cities ?? and why is a foreach loop used to set it (to whatever happens to be the last city.OpenTime) ?? in a method called Show... where nothing gets shown :confused::confused:

                          Luc Pattyn [My Articles]

                          A 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            Anka_Ame wrote:

                            foreach (City cityhour in this) { startHour = cityhour.OpenTime; }

                            What does the variable startHour (and the property StartHour) mean in a list of cities ?? and why is a foreach loop used to set it (to whatever happens to be the last city.OpenTime) ?? in a method called Show... where nothing gets shown :confused::confused:

                            Luc Pattyn [My Articles]

                            A Offline
                            A Offline
                            Anka_Ame
                            wrote on last edited by
                            #15

                            that's not my problem. I just want to read the value from a Xml file and to convert it to time, in a string without date.

                            M 1 Reply Last reply
                            0
                            • A Anka_Ame

                              that's not my problem. I just want to read the value from a Xml file and to convert it to time, in a string without date.

                              M Offline
                              M Offline
                              Malcolm Smart
                              wrote on last edited by
                              #16

                              You can't have a time without a date. You keep using TimeSpan. That shows the difference between two times / dates - unless I am mistaken. Anybody? You have two options - convert all times to 'today' + your time. You can then sort them. or Or convert your timestrnig to an integer, throw all the ints into a collection and sort them. string myString = "07:30" int myTime = int.Parse( myString.Substring(0,2) ) * 60; myTime += int.Parse( myString.Substring(3,2) ) ; This will give you the time in minutes, as aninterger. (from memory, so could be typo's etc);

                              Regards Angel *********************************************

                              1 Reply Last reply
                              0
                              • A Anka_Ame

                                Hi, I have a problem with the conversion of a string of type "07:30" into a time. I want to take only the time, without the date. I tried DateTime openTime = DateTime.Now.ToShortTimeString(open); ,where open = "07:30". Please help me with this problem.

                                A Offline
                                A Offline
                                AFSEKI
                                wrote on last edited by
                                #17

                                Use TimeSpan.Parse(string s) Hope this helps...

                                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