0

How to get the mobile current date time with the 12/24 format .Date and time e.g. April 17, 2020 - 1:15 PM. The date and the time should conform to the preferences the user has set on the device.

how we do it in Xamarin forms.If i write the below in android then how to call in Xamarin forms.

If mobile set to 24 hours then it should display Any one can help me with this.

This is the android snippet Xamarin Forms how can I do it that if I write the below snippet in android then how can I call this in Xamarin forms?

 var date = DateTime.Now.ToLocalTime();
 var is24hour = Android.Text.Format.DateFormat.Is24HourFormat(this);
 var format = is24hour ? "dd/MM/yyyy HH:mm" : "dd/MM/yyyy hh:mm tt";
 var dtString = date.ToString(format);

This is how it look like shows in 24 hours format Device setting in 24 hours format Code screen shot

omi.iqb
  • 31
  • 5
  • you shouldn't need to do anything. .NET should automatically respect the locale settings of the device. If you just output `DateTime.Now.ToString()` it should reflect that. – Jason Jun 27 '20 at 15:10
  • @Jason i also need to check the datetime preferences whether it is in 24hours format or 12 hours format? – omi.iqb Jun 27 '20 at 15:12
  • it should do that automatically – Jason Jun 27 '20 at 15:16
  • @Jason no i tried this is not as i expected result. – omi.iqb Jun 27 '20 at 15:27
  • please edit your question to show what you tried, the actual result, and the expected result – Jason Jun 27 '20 at 15:38
  • @Jason please see the attached screenshots – omi.iqb Jun 27 '20 at 15:50
  • as best as I can tell, most Asian countries default to 24 hour time, so it may be using that default and ignoring the 24 hour setting. You can override that using a format string – Jason Jun 27 '20 at 15:57
  • @Gusman I tried this it works but it only show Time not date but if i change device setting to 24 hours it relfect vice versa Thank you it work i will check it for date later thanks once again – omi.iqb Jun 27 '20 at 17:06

0 Answers0