You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已知UWP桌面用ShortTimePattern获时间格式,如何在Xamarin.Forms各平台实现?

获取Xamarin.Forms各平台设备时间格式的实现方案

Got it, let's walk through how to retrieve the device's short time format across Xamarin.iOS, Android, and Windows (UWP) platforms. Since you already have the UWP piece covered, I'll dive into the other two first, then wrap up with a way to unify this in your shared Xamarin.Forms code.

Xamarin.iOS

On iOS, we can leverage NSDateFormatter with the system's current locale to get the exact short time format set by the user. Here's a straightforward implementation:

using Foundation;

public class iOSDateFormatService : ITimeFormatService
{
    public string GetShortTimeFormat()
    {
        var currentLocale = NSLocale.CurrentLocale;
        var dateFormatter = new NSDateFormatter
        {
            TimeStyle = NSDateFormatterStyle.Short,
            Locale = currentLocale
        };
        return dateFormatter.DateFormat;
    }
}

This will return strings like h:mm a (12-hour format with AM/PM) or HH:mm (24-hour format) depending on the device's system settings.

Xamarin.Android

For Android, we can use the platform's DateFormat class to fetch the system's preferred short time pattern. There are a couple of reliable ways to do this:

using Android.Text.Format;
using Android.Content.Res;

public class AndroidDateFormatService : ITimeFormatService
{
    public string GetShortTimeFormat()
    {
        var context = Android.App.Application.Context;
        // Option 1: Get the exact pattern used by the system's time formatter
        var systemTimeFormatter = DateFormat.GetTimeFormat(context);
        return systemTimeFormatter.ToString();

        // Option 2: Use GetBestDateTimePattern for locale-specific formatting
        // var locale = Resources.System.Configuration.Locale;
        // return DateFormat.GetBestDateTimePattern(locale, "Hm");
    }
}

Both options work well—Option 1 directly uses the system's built-in time formatter, while Option 2 lets you specify a template ("Hm" for short time) to get the most appropriate pattern for the device's locale.

Windows (UWP)

As you mentioned, UWP makes this easy with DateTimeFormatInfo.ShortTimePattern:

using System.Globalization;

public class UwpDateFormatService : ITimeFormatService
{
    public string GetShortTimeFormat()
    {
        return CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
    }
}

This pulls the short time format directly from the user's current culture settings, which matches what the system displays.

Unify with DependencyService

To use these platform-specific implementations in your shared Xamarin.Forms code, first define an interface:

public interface ITimeFormatService
{
    string GetShortTimeFormat();
}

Then register each platform's implementation (don't forget to add the [assembly: Dependency(typeof(PlatformService))] attribute above each service class), and call it from your shared code like this:

var deviceTimeFormat = DependencyService.Get<ITimeFormatService>().GetShortTimeFormat();

That's all! This setup will give you the exact time format the device uses, no matter which platform you're running on.

内容的提问来源于stack exchange,提问作者Jeya Sri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:17:48