Results 1 to 8 of 8

Thread: [RESOLVED] time only help please?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Resolved [RESOLVED] time only help please?

    Hi,

    I've got a little issue that some kind person here is bound to know how to solve..

    I have a drop down list with some open / closed times in it in the format of 00:00 I want to compare this value against the time now?

    I cannot use
    Code:
    mon_skillSet.openTo > DateTime.Now.TimeOfDay.ToString()
    as > sign callot evaluate strings..

    when I (DateTime)mon_skillSet.openTo I also get an error.

    Any help muchly appreciated.

    Thanks

    chubby.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: time only help please?

    The obvious answer is to convert the string on the left to a TimeSpan instead of the TimeSpan on the right to a string. Then you CAN use '>' to compare the two values. The TimeSpan structure has methods for parsing strings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: time only help please?

    Quote Originally Posted by jmcilhinney
    The obvious answer is to convert the string on the left to a TimeSpan instead of the TimeSpan on the right to a string. Then you CAN use '>' to compare the two values. The TimeSpan structure has methods for parsing strings.
    Thanks,

    I am sorry to be a pain, I cannot see the TimeSpan methods that you mention. using (TimeSpan)mon_skillSet.openTo throws an error (as expected), the overload of new YimeSpan(xx,xx,xx) would work but it takes me into the realms of having to split up my exsiting string into an array?

    Is that really necessary?

    Thanks again.

    chub.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: time only help please?

    Code:
                            string[] arrOpenFromTime = new string[2];
                            arrOpenFromTime = mon_skillSet.openFrom.Split(":");
                            TimeSpan tsOF = new TimeSpan((int)arrOpenFromTime[0],(int)arrOpenFromTime[1]);
    
                            string[] arrOpenToTime = new string[2];
                            arrOpenToTime = mon_skillSet.openTo.Split(":");
                            TimeSpan tsOT = new TimeSpan((int)arrOpenToTime[0],(int)arrOpenToTime[1]);
    
                            if ((tsOF > DateTime.Now.TimeOfDay) && (DateTime.Now.TimeOfDay < tsOT))
                            {
                                //blah blah blah..
                            }
    This is yet to be tested, but its the best I can come up with.... any other way of doing it ? Seems massively painful for such a simple compare...

    Thanks

    chub.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: time only help please?

    If mon_skillSet.openTo is a satring then you can't cast it as type TimeSpan. If it isn't a TimeSpan then you can't just say that it is. What did I post before?
    The TimeSpan structure has methods for parsing strings.
    So, when you went to the MSDN library documentation for the TimeSpan structure, what methods did you see? Given that the purpose is to parse a string, I would think that the Parse method would have piqued your interest, and the TryParse method should have been worth a look too. You did read the help for the TimeSpan structure didn't you?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: time only help please?

    Quote Originally Posted by jmcilhinney
    If mon_skillSet.openTo is a satring then you can't cast it as type TimeSpan. If it isn't a TimeSpan then you can't just say that it is.
    Ahhh the dry-wit of sarcasm.... I am quite aware that this is not possible.

    What did I post before?So, when you went to the MSDN library documentation for the TimeSpan structure, what methods did you see? Given that the purpose is to parse a string, I would think that the Parse method would have piqued your interest, and the TryParse method should have been worth a look too. You did read the help for the TimeSpan structure didn't you?
    Err.. there you have me (as you are also aware), I traveresed the intellisense structure looking for something more descriptive... I am, as you may have potentially guessed, learning as I go with this language and had yet to encounter the paradigm of Parse as it relates to strings.

    Thank you for your help (and obvious patience). it is much appreciated.

    chub.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] time only help please?

    If you want to do something with or to an object then by all means have a quick look at Intellisense, but if that doesn't reveal anything obvious then you should immediately read the documentation for the type overview and member listing. Even if you didn't know what parsing was, descriptions like:
    Constructs a new TimeSpan object from a time interval specified in a string.
    would have told you pretty quickly that that was the method you needed. Microsoft have spent a lot of time and money creating the MSDN library documentation. Don't ignore it because it will answer most of your questions for you. It really comes down to RTFM, and if we can't do it then what hope that our users will?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: [RESOLVED] time only help please?

    Quote Originally Posted by jmcilhinney
    If you want to do something with or to an object then by all means have a quick look at Intellisense, but if that doesn't reveal anything obvious then you should immediately read the documentation for the type overview and member listing. Even if you didn't know what parsing was, descriptions like:would have told you pretty quickly that that was the method you needed. Microsoft have spent a lot of time and money creating the MSDN library documentation. Don't ignore it because it will answer most of your questions for you. It really comes down to RTFM, and if we can't do it then what hope that our users will?
    Totally agree, I have no excuses. Should have RTFM'd.

    chub.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width