What happened to .ToShortDateString in .NET Portable Class Library

落爺英雄遲暮 提交于 2019-12-23 07:26:34

问题


I am wondering why there is no .ToShortDateString in the .NET Portable Class Library. I have 2 projects (Silverlight, and regular .NET Class Library) that use the same code, and the code involves calling .ToShortDateString() on a DateTime object. In order to reuse the same code instead of copying it in 2 places, I created a portable class library so it can be imported by both Silverlight and .NET Class Library. Unfortunately, it doesn't seem like .ToShortDateString() is available when using the class library. I can accept a string parameter in the portable class library method and pass the .ToShortDateString() value from both silverlight and class library projects, but I am wondering why this method isn't native for the portable library. Is it a culture issue?


回答1:


While most of methods/properties that belong to types defined in System namespace are available in PCLs, there are some exceptions, and ToShortDateString is one of them. Below is the list of portable DateTime members. I don't know what was the reason behind the exclusion of some string conversion methods, but I guess this is due to redundancy. As cadrell0 pointed out, you can always achieve the same by using ToString with a parameter.

T:System.DateTime
M:System.DateTime.ToString(System.String)
M:System.DateTime.op_GreaterThan(System.DateTime,System.DateTime)
M:System.DateTime.ParseExact(System.String,System.String[],System.IFormatProvider,System.Globalization.DateTimeStyles)
M:System.DateTime.get_Month
M:System.DateTime.FromFileTimeUtc(System.Int64)
M:System.DateTime.get_Date
M:System.DateTime.get_TimeOfDay
M:System.DateTime.get_Kind
M:System.DateTime.ToUniversalTime
M:System.DateTime.get_Year
M:System.DateTime.op_Subtraction(System.DateTime,System.TimeSpan)
M:System.DateTime.get_Second
M:System.DateTime.get_DayOfWeek
M:System.DateTime.TryParse(System.String,System.IFormatProvider,System.Globalization.DateTimeStyles,System.DateTime@)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32)
M:System.DateTime.get_Day
P:System.DateTime.Date
M:System.DateTime.op_Addition(System.DateTime,System.TimeSpan)
M:System.DateTime.IsDaylightSavingTime
M:System.DateTime.get_DayOfYear
M:System.DateTime.ToFileTime
M:System.DateTime.Subtract(System.DateTime)
M:System.DateTime.IsLeapYear(System.Int32)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.DateTimeKind)
M:System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider,System.Globalization.DateTimeStyles)
P:System.DateTime.Day
M:System.DateTime.get_Hour
M:System.DateTime.Equals(System.DateTime)
M:System.DateTime.get_UtcNow
M:System.DateTime.get_Today
M:System.DateTime.TryParse(System.String,System.DateTime@)
P:System.DateTime.Kind
M:System.DateTime.System#IComparable#CompareTo(System.Object)
P:System.DateTime.UtcNow
P:System.DateTime.Hour
P:System.DateTime.Millisecond
M:System.DateTime.Parse(System.String)
F:System.DateTime.MinValue
M:System.DateTime.op_GreaterThanOrEqual(System.DateTime,System.DateTime)
M:System.DateTime.#ctor(System.Int64,System.DateTimeKind)
M:System.DateTime.GetHashCode
P:System.DateTime.Year
M:System.DateTime.Add(System.TimeSpan)
M:System.DateTime.Equals(System.DateTime,System.DateTime)
M:System.DateTime.ToString(System.IFormatProvider)
M:System.DateTime.get_Now
P:System.DateTime.Month
M:System.DateTime.DaysInMonth(System.Int32,System.Int32)
M:System.DateTime.AddMinutes(System.Double)
M:System.DateTime.get_Minute
M:System.DateTime.#ctor(System.Int64)
M:System.DateTime.op_LessThanOrEqual(System.DateTime,System.DateTime)
M:System.DateTime.ToString(System.String,System.IFormatProvider)
P:System.DateTime.DayOfYear
M:System.DateTime.AddMilliseconds(System.Double)
P:System.DateTime.Second
P:System.DateTime.DayOfWeek
M:System.DateTime.op_Equality(System.DateTime,System.DateTime)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32)
M:System.DateTime.TryParseExact(System.String,System.String,System.IFormatProvider,System.Globalization.DateTimeStyles,System.DateTime@)
M:System.DateTime.ToFileTimeUtc
P:System.DateTime.Today
M:System.DateTime.op_LessThan(System.DateTime,System.DateTime)
M:System.DateTime.get_Millisecond
M:System.DateTime.op_Subtraction(System.DateTime,System.DateTime)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32)
M:System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider)
M:System.DateTime.AddSeconds(System.Double)
M:System.DateTime.AddMonths(System.Int32)
M:System.DateTime.AddYears(System.Int32)
M:System.DateTime.Parse(System.String,System.IFormatProvider,System.Globalization.DateTimeStyles)
M:System.DateTime.get_Ticks
P:System.DateTime.Ticks
M:System.DateTime.TryParseExact(System.String,System.String[],System.IFormatProvider,System.Globalization.DateTimeStyles,System.DateTime@)
M:System.DateTime.ToLocalTime
M:System.DateTime.op_Inequality(System.DateTime,System.DateTime)
M:System.DateTime.SpecifyKind(System.DateTime,System.DateTimeKind)
M:System.DateTime.AddHours(System.Double)
P:System.DateTime.Minute
M:System.DateTime.Subtract(System.TimeSpan)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.DateTimeKind)
F:System.DateTime.MaxValue
M:System.DateTime.ToString
M:System.DateTime.FromFileTime(System.Int64)
P:System.DateTime.TimeOfDay
M:System.DateTime.Compare(System.DateTime,System.DateTime)
M:System.DateTime.CompareTo(System.DateTime)
M:System.DateTime.Parse(System.String,System.IFormatProvider)
M:System.DateTime.AddDays(System.Double)
P:System.DateTime.Now
M:System.DateTime.Equals(System.Object)
M:System.DateTime.AddTicks(System.Int64)



回答2:


It was removed to deemphasize its use from what we consider the "modern" surface area, which I hint about here (What is .NET Portable Subset (Legacy)?). This means that it does not show up newer platforms (such as Windows Store apps) and does not show up in portable libraries.

You can mimic its behavior by simply passing "d" to DateTime.ToString().

We wanted to deemphasize its use because it is the only .NET Framework date format that does not have a representation at the Windows OS level. This causes it to not reflect/respect the formatting changes made by the user. In certain organizations and governments, it is important that these settings are respected.



来源:https://stackoverflow.com/questions/18518004/what-happened-to-toshortdatestring-in-net-portable-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!