2016-06-15 10 views
8

DateTimeオブジェクトをPCL Xamarin.Formsプロジェクトを実行しているときに、デバイスのデフォルトのdatetime形式の文字列にフォーマットするにはどうすればよいですか?フォーマットXamarinフォームのデバイスフォーマット文字列

このthreadおよびbugに従ってMSDN要件に従ってDateTime.ToShortString()が機能しません。

フォームベースのソリューションはありますか、それともプラットフォーム固有のプロジェクトから取得する必要がありますか? Android用

、私はDIを使用してネイティブプロジェクトから、次の操作を行うことができます

String format = Settings.System.GetString(this.context.ContentResolver 
             , Settings.System.DateFormat); 
string shortDateString = dateTime.ToString(format); 

または私はあまりにも(下記のコードのC#バージョンを)これを使用することができます:

DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context); 

を調べてくださいthisこれは要件をより明確に理解するために質問されています(Androidの場合のみ、Xamarin.Formsの質問のようにすべてのプラットフォームで必要です)。

XamarinフォームのDatePickerTimePickerには、デバイスフォーマットの日付と時刻が表示されているので、PCLで取得する方法があると思います。

はまた、私は、私は要件を実装するためにDIを使用していたすべてのPCLの実装を見つけることができなかったようなど

+0

フォーマットがクライアント固有である場合は、依存関係サービスを使用することをお勧めします。 https://developer.xamarin.com/guides/xamarin-forms/dependency-service/introduction/ –

+0

@AndresCastro - ありがとうございますが、私はPCLからオプションを探しています。はい、それはそれぞれのためにDIを使って行うことができますプラットフォーム。 –

+2

Gotcha、私はdateTime.ToString( "d")を使用するだけで、現在の文化のToStringを適用する必要があると思うでしょう。私は正直なところ、それをテストしていない。デバイスカルチャを変更したときに何が起こるかを知ることは興味深いかもしれません。 –

答えて

2

プラットフォーム、イディオム、などの情報を提供していPCLでDeviceクラスがあります。 PCLで

使用法:

DependencyService.Get<IDeviceInfoService>()?.ConvertToDeviceTimeFormat(DateTime.Now);  
DependencyService.Get<IDeviceInfoService>()?.ConvertToDeviceTimeFormat(DateTime.Now); 

PCL:

public interface IDeviceInfoService 
{ 
    string ConvertToDeviceShortDateFormat(DateTime inputDateTime);  
    string ConvertToDeviceTimeFormat(DateTime inputDateTime); 
} 

アンドロイド:

[assembly: Dependency(typeof(DeviceInfoServiceImplementation))] 
namespace Droid.Services 
{ 
    public class DeviceInfoServiceImplementation : IDeviceInfoService 
    { 
     public string ConvertToDeviceShortDateFormat(DateTime inputDateTime) 
     { 
      var dateFormat = Android.Text.Format.DateFormat.GetDateFormat(Android.App.Application.Context); 
      var epochDateTime = Helper.ConvertDateTimeToUnixTime(inputDateTime, true); 

      if (epochDateTime == null) 
      { 
       return string.Empty; 
      } 

      using (var javaDate = new Java.Util.Date((long)epochDateTime)) 
      { 
       return dateFormat.Format(javaDate); 
      } 
     } 

     public string ConvertToDeviceTimeFormat(DateTime inputDateTime) 
     { 
      var timeFormat = Android.Text.Format.DateFormat.GetTimeFormat(Android.App.Application.Context); 
      var epochDateTime = Helper.ConvertDateTimeToUnixTime(inputDateTime, true); 

      if (epochDateTime == null) 
      { 
       return string.Empty; 
      } 

      using (var javaDate = new Java.Util.Date((long)epochDateTime)) 
      { 
       return timeFormat.Format(javaDate); 
      } 
     } 
    } 
} 

のiOS:

[assembly: Dependency(typeof(DeviceInfoServiceImplementation))] 
namespace iOS.Services 
{ 
    public class DeviceInfoServiceImplementation : IDeviceInfoService 
    { 
     public string ConvertToDeviceShortDateFormat(DateTime inputDateTime) 
     { 
      var timeInEpoch = Helper.ConvertDateTimeToUnixTime(inputDateTime); 

      if (timeInEpoch == null) 
      { 
       return string.Empty; 
      } 

      using (var dateInNsDate = NSDate.FromTimeIntervalSince1970((double)timeInEpoch)) 
      { 
       using (var formatter = new NSDateFormatter 
       { 
        TimeStyle = NSDateFormatterStyle.None, 
        DateStyle = NSDateFormatterStyle.Short, 
        Locale = NSLocale.CurrentLocale 
       }) 
       { 
        return formatter.ToString(dateInNsDate); 
       } 
      } 
     } 

     public string ConvertToDeviceTimeFormat(DateTime inputDateTime) 
     { 
      var timeInEpoch = Helper.ConvertDateTimeToUnixTime(inputDateTime); 

      if (timeInEpoch == null) 
      { 
       return string.Empty; 
      } 

      using (var dateInNsDate = NSDate.FromTimeIntervalSince1970((double)timeInEpoch)) 
      { 
       using (var formatter = new NSDateFormatter 
       { 
        TimeStyle = NSDateFormatterStyle.Short, 
        DateStyle = NSDateFormatterStyle.None, 
        Locale = NSLocale.CurrentLocale 
       }) 
       { 
        return formatter.ToString(dateInNsDate); 
       } 
      } 
     } 
    } 
} 

Windowsの場合:

[assembly: Dependency(typeof(DeviceInfoServiceImplementation))] 
namespace WinPhone.Services 
{ 
    public class DeviceInfoServiceImplementation : IDeviceInfoService 
    { 
     public string ConvertToDeviceShortDateFormat(DateTime inputDateTime) 
     { 
      return inputDateTime.ToShortDateString(); 
     } 

     public string ConvertToDeviceTimeFormat(DateTime inputDateTime) 
     { 
      return inputDateTime.ToShortTimeString(); 
     } 
    } 
} 

ヘルパーメソッド:現在のXamarinは、フォームのバージョンで

private static readonly DateTime EpochDateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 
public static long? ConvertDateTimeToUnixTime(DateTime? date, bool isDatarequiredInMilliSeconds = false, DateTimeKind dateTimeKind = DateTimeKind.Local) => date.HasValue == false 
      ? (long?)null 
      : Convert.ToInt64((DateTime.SpecifyKind(date.Value, dateTimeKind).ToUniversalTime() - EpochDateTime).TotalSeconds) * (isDatarequiredInMilliSeconds ? 1000 : 1); 
0

、あなたが試すことがあります。

// This does not work with PCL 
var date1 = DateTime.Now.ToShortDateString(); 

これは、デバイスのロケールに固有の形式で日付を与えますプラットフォーム間で動作します。また

var date1 = DateTime.Now.ToString(CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern); 

特定の形式については、以下を試すことができます。

var date1 = DateTime.Now.ToString("dd-MM-yyyy"); 

最初と最後の1は私にはかなりクールに見えます。しかし、2番目と3番目のオプションだけがPCLで動作します。

関連する問題