Forum Xamarin Xamarin.Forms

Get TimePicker value as String?

I can't believe this is hard information to find. I must be searching with the wrong words or something. Everybody's talking about binding this and binding that--I just want a string!

When I try to get the Time value out of my TimePicker it just says "00:00:00". Halp.


Best Answers


  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Time picker is going to give you a DateTime object. Period. End. That's all she wrote.

    If you want to take a .ToString() off that... go for it.
    If you want to apply a .String.Format to it to get your string formatted in a particular way... You can do that to.

    But without your code, or screenshots of run-time values while you're working... Not much to tell you about what's going wrong.

    As for bindings... What most people do is store the DateTime object in their ViewModel. C# is Object Oriented. We like objects. We keep objects. When it comes time to display that someplace... Then you bind to the object and format it as a string however you like. But you store objects so you can do whatever you like with it. If you want it as a 24hour string in one place and a 12hr string on a different view you can do that without having multiple string properties.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I just remembered... In this 2017 tutorial of mine one of hte objects for the demo app is the birthday of your pet. That's a DateTime and shown as a string.

  • CalmBreathsSlowPulseCalmBreathsSlowPulse Member ✭✭
    edited August 2018

    @ClintStLaurent Thanks for trying to help.

    a) TimePicker does not give me a DateTime object, period, end, all she wrote. All I can see in the autocomplete is the Time value, which is a TimeSpan. Using TimeSpan.ToString() returns "00:00:00".

    b) You have linked to the start of a multi-multi-page article; as far as I can see there's no way to search it and there's no easily-discoverable download link to the completed project. I do appreciate the attempt to help but I'm looking for--I think, I hope, I mean for goodness' sake it ought to be--just one or two lines of code. Could you perhaps provide a link just to that?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Yep. My mistake. TimePicker provides a TimeSpan.

    DatePicker provides a DateTime object.

    But that doesn't change the concept. You shouldn't be working with string properties throughout your ViewModel. Just keep the real object. In your XAML you can format it however you like using Format in the XAML.

    You have linked to the start of a multi-multi-page article

    Yes. The idea is you take an hour and work the tutorial series and you build the sample project. In the process of that hour you learn the concepts. Its not meant to be a download you copy/paste from. Let's be honest; You've spent well more than an hour trying to find a fast fix. Sometimes you just have to put in the work to learn rather than google. No offense.

  • @ClintStLaurent I respect that you are trying to encourage best practices, but aren't you also kind of holding the actual answer hostage to try to make me do it your way? I understand why, I know MVVM is today's potato. The tutorial I am using to get up to speed on Xamarin is a Udemy course, and the guy does teach MVVM, in fact, but he specifically points out that it's a lot of extra overhead for small single-person projects, and recommends avoiding it in cases like that.

    @seanyda thanks for the help. The first example is pretty much the exact thing that returns "00:00:00" in my code. Maybe I'm doing something wrong somewhere else. Thanks for the MVVM example too, if I do end up going that way I'll investigate it.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited August 2018

    @CalmBreathsSlowPulse said:
    @ClintStLaurent I respect that you are trying to encourage best practices, but aren't you also kind of holding the actual answer hostage to try to make me do it your way?

    [Edited by Clint] Took out all the wordiness.
    No. And I really, really, really resent the accusation. You just haven't given anyone anything to work with. I don't have an answer for you because you haven't provided a single line of your own code. How could anyone have answer for you? The best anyone can do in that case is give general suggestions.

    I honestly wish you the best of luck with your project. Really. I hope it all gets worked out. I'm backing out of this conversation though. Before I get insulted further for trying to help someone.

  • @ClintStLaurent I'm genuinely sorry. I didn't mean to, but I see that my words came off as insulting. I know you are giving of your time and expertise for free, and that's a noble thing, and good for everyone. No one needs an online hassle in their day, it sucks, and I'm sorry. There are some more straightforward answers in the other replies, but I do get what you're trying to do, you're coming from a place of what you think is best, and that's a good and generous impulse. Thank you. And again I apologize.

    @JoeManke that's it! Wow man they make that hard. You a saint.

Sign In or Register to comment.