how to get Date and Time in correct format

Priya2018Priya2018 USMember ✭✭

Hello,

in my first App in xamarin.forms (iOS and android), i am using DatePicker and TimePicker.
i am getting selected Date as : 28-08-2018 and selected Time in 24 Format like 23:20
i wanted to get Date and Time in below Format:

08-28-2018 11:20 PM

how to achieve this... from selected date and time.

Answers

  • ApurvaXamApurvaXam Member ✭✭

    hello @Priya2018 you can use StringFormat for getting date and time in the format you specified.
    use format specified in your xaml Lable where you are displaying selected date and time
    for date - StringFormat='{}{0:dd-MMM-yyyy}'}"
    for time - StringFormat='{}{0:h:mm tt}'}"

  • Priya2018Priya2018 USMember ✭✭

    @ApurvaXam said:
    hello @Priya2018 you can use StringFormat for getting date and time in the format you specified.
    use format specified in your xaml Lable where you are displaying selected date and time
    for date - StringFormat='{}{0:dd-MMM-yyyy}'}"
    for time - StringFormat='{}{0:h:mm tt}'}"

    sorry i am very new to xamarin , can u plz show example.... i tried to use but where should i set in Datepicker & Timepicker view, i have added both from xml?

  • ApurvaXamApurvaXam Member ✭✭

    @Priya2018 how you are displaying date and time? please post your code snippet

  • Priya2018Priya2018 USMember ✭✭
    edited August 2018

    @ApurvaXam said:
    @Priya2018 how you are displaying date and time? please post your code snippet

    i am not displaying once it is selected and click on Done it shows me 28-08-2018 format.
    i don't know how to convert Date and Time in required format (08-28-2018 11:20 PM)which i mentioned and display as String or Datetime.

    however i am using Unfocused event to check date/time.

        void Handle_Unfocused(object sender, Xamarin.Forms.FocusEventArgs e)
        {
            DatePicker datePicker = sender as DatePicker;
        }
    
        void Handle_Unfocused_1(object sender, Xamarin.Forms.FocusEventArgs e)
        {
            TimePicker timePicker = sender as TimePicker;
       }
    
  • ApurvaXamApurvaXam Member ✭✭
    edited August 2018

    @Priya2018 For time it will show 24 hr format as TimePicker comes in 24 format only, while displaying selected time in other label you can change time format in 12hrs.
    Use format in Datepicker and Timepicker control in xaml as,
    DatePicker Format="dd-MMM-yyyy" or DatePicker Format="dd-MM-yyyy"
    TimePicker Format="HH:mm"

Sign In or Register to comment.