将Xamarin表单中的DateTime格式化为设备格式字符串
在运行PCL Xamarin.Forms项目时,如何将DateTime
对象格式化为设备默认日期时间格式的字符串,我的部署目标包括iOS,Android和Windows.
How can I format a DateTime
object to a string in the device default datetime format when running a PCL Xamarin.Forms project and my deployement targets include iOS, Android and Windows.
是否有任何基于Forms的解决方案,或者我需要从特定于平台的项目中获取它?
Is there any Forms based solution or do I need to get it from platform specific projects?
对于Android,我可以使用DI在Native项目中执行以下操作:
For Android, I can do the following from Native project using DI:
String format = Settings.System.GetString(this.context.ContentResolver
, Settings.System.DateFormat);
string shortDateString = dateTime.ToString(format);
或者我也可以使用此代码(以下代码的C#版本):
OR I can use this too (the C# version of the below code):
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
查看此 SO问题以便理解要求更明确(仅适用于android,我希望适用于所有平台,因为这是Xamarin.Forms问题).
Look into this SO question to understand the requirement more clearly (its only for android, I want it for all platforms as this is a Xamarin.Forms question).
由于Xamarin表单中的DatePicker
和TimePicker
以设备格式显示日期和时间,因此我希望有一种方法可以在PCL中获取它.
Since the DatePicker
and TimePicker
in Xamarin Forms show the date and time in device format I am hoping there would a way to get it in the PCL.
PCL中还有一个Device
类,其中包含诸如平台,成语等信息.
Also there is a Device
class in PCL which has information like platforms, idiom, etc.
由于找不到任何PCL实现,因此我使用DI来实现要求.
As I could not find any PCL implementation I used DI to implement the requirement.
在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);
}
Android:
[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();
}
}
}
Helper方法:
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);