本文整理汇总了C#中DateTime.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.ToString方法的具体用法?C# DateTime.ToString怎么用?C# DateTime.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestSettingThreadCultures
public void TestSettingThreadCultures()
{
RemoteInvoke(() =>
{
CultureInfo culture = new CultureInfo("ja-JP");
CultureInfo.CurrentCulture = culture;
DateTime dt = new DateTime(2014, 3, 14, 3, 14, 0);
Assert.Equal(dt.ToString(), dt.ToString(culture));
Assert.Equal(dt.ToString(), dt.ToString(culture.DateTimeFormat));
return SuccessExitCode;
}).Dispose();
}
示例2: X509Cert2Test
public static void X509Cert2Test()
{
string certName = @"[email protected], CN=ABA.ECOM Root CA, O=""ABA.ECOM, INC."", L=Washington, S=DC, C=US";
DateTime notBefore = new DateTime(1999, 7, 12, 17, 33, 53, DateTimeKind.Utc).ToLocalTime();
DateTime notAfter = new DateTime(2009, 7, 9, 17, 33, 53, DateTimeKind.Utc).ToLocalTime();
using (X509Certificate2 cert2 = new X509Certificate2(Path.Combine("TestData", "test.cer")))
{
Assert.Equal(certName, cert2.IssuerName.Name);
Assert.Equal(certName, cert2.SubjectName.Name);
Assert.Equal("ABA.ECOM Root CA", cert2.GetNameInfo(X509NameType.DnsName, true));
PublicKey pubKey = cert2.PublicKey;
Assert.Equal("RSA", pubKey.Oid.FriendlyName);
Assert.Equal(notAfter, cert2.NotAfter);
Assert.Equal(notBefore, cert2.NotBefore);
#if netstandard17
Assert.Equal(notAfter.ToString(), cert2.GetExpirationDateString());
Assert.Equal(notBefore.ToString(), cert2.GetEffectiveDateString());
#endif
Assert.Equal("00D01E4090000046520000000100000004", cert2.SerialNumber);
Assert.Equal("1.2.840.113549.1.1.5", cert2.SignatureAlgorithm.Value);
Assert.Equal("7A74410FB0CD5C972A364B71BF031D88A6510E9E", cert2.Thumbprint);
Assert.Equal(3, cert2.Version);
}
}
示例3: ConversionUtilUsesTypeConverterToConvertArbitraryTypes
public void ConversionUtilUsesTypeConverterToConvertArbitraryTypes()
{
// Arrange
var date = new DateTime(2010, 01, 01);
string original = date.ToString(CultureInfo.InvariantCulture);
// Act
object result;
bool success = ConversionUtil.TryFromString(typeof(DateTime), original, out result);
// Assert
Assert.True(success);
Assert.Equal(date, result);
}
示例4: ConvertTo_WithContext
public static void ConvertTo_WithContext()
{
DateTimeFormatInfo formatInfo = (DateTimeFormatInfo)CultureInfo.CurrentCulture.GetFormat(typeof(DateTimeFormatInfo));
string formatWithTime = formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern;
string format = formatInfo.ShortDatePattern;
DateTime testDateAndTime = new DateTime(1998, 12, 5, 22, 30, 30);
ConvertTo_WithContext(new object[5, 3]
{
{ DateTimeConverterTests.s_testDate, DateTimeConverterTests.s_testDate.ToString(format, CultureInfo.CurrentCulture), null },
{ testDateAndTime, testDateAndTime.ToString(formatWithTime, CultureInfo.CurrentCulture), null },
{ DateTime.MinValue, string.Empty, null },
{ DateTimeConverterTests.s_testDate, "1998-12-05", CultureInfo.InvariantCulture },
{ testDateAndTime, "12/05/1998 22:30:30", CultureInfo.InvariantCulture }
},
DateTimeConverterTests.s_converter);
}
示例5: GenerateWarnings
void GenerateWarnings(StringBuilder sb, DateTime lastFlush, int discardedSinceLastFlush,
int seq, int eventsToDrop) {
if (!UseBuffering) {
return;
}
bool headerAdded = false;
bool hasWarnings = false;
// This warning is issued only in the 1st message (vswhidbey 217578)
if (discardedSinceLastFlush != 0 && seq == MessageSequenceBase) {
sb.Append(s_header_warnings);
sb.Append("\n");
sb.Append(_separator);
headerAdded = true;
sb.Append(SR.GetString(SR.MailWebEventProvider_discard_warning,
MessageIdDiscard.ToString(CultureInfo.InstalledUICulture),
discardedSinceLastFlush.ToString(CultureInfo.InstalledUICulture),
lastFlush.ToString("r", CultureInfo.InstalledUICulture)));
sb.Append("\n\n");
hasWarnings = true;
}
if (eventsToDrop > 0) {
if (!headerAdded) {
sb.Append(s_header_warnings);
sb.Append("\n");
sb.Append(_separator);
headerAdded = true;
}
sb.Append(SR.GetString(SR.MailWebEventProvider_events_drop_warning,
MessageIdEventsToDrop.ToString(CultureInfo.InstalledUICulture),
eventsToDrop.ToString(CultureInfo.InstalledUICulture)));
sb.Append("\n\n");
hasWarnings = true;
}
if (hasWarnings) {
sb.Append("\n");
}
}
示例6: FormatDate
/// <summary>
/// Convert a date to a string.
///
/// Some cultures, specifically using the Um-AlQura calendar cannot convert dates far into
/// the future into strings. If the expiration date of an X.509 certificate is beyond the range
/// of one of these cases, we need to fall back to a calendar which can express the dates
/// </summary>
internal static string FormatDate(DateTime date)
{
CultureInfo culture = CultureInfo.CurrentCulture;
if (!culture.DateTimeFormat.Calendar.IsValidDay(date.Year, date.Month, date.Day, 0))
{
// The most common case of culture failing to work is in the Um-AlQuara calendar. In this case,
// we can fall back to the Hijri calendar, otherwise fall back to the invariant culture.
if (culture.DateTimeFormat.Calendar is UmAlQuraCalendar)
{
culture = culture.Clone() as CultureInfo;
culture.DateTimeFormat.Calendar = new HijriCalendar();
}
else
{
culture = CultureInfo.InvariantCulture;
}
}
return date.ToString(culture);
}
示例7: getTimeStampText
private static string getTimeStampText(DateTime dateTime)
{
if (dateTime.TimeOfDay == TimeSpan.Zero)
return dateTime.ToString("D");
return dateTime.ToString("f");
}
示例8: DPClearWMTB
public void DPClearWMTB()
{
_elementToCleanUp = new DatePicker();
_elementToCleanUp.Loaded += new RoutedEventHandler(_elementToCleanUp_Loaded);
TestPanel.Children.Add(_elementToCleanUp);
EnqueueConditional(IsLoaded);
DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
DateTime d = new DateTime(2003, 10, 10);
EnqueueCallback(delegate
{
_elementToCleanUp.SelectedDate = d;
});
EnqueueSleep(sleepTime);
EnqueueCallback(delegate
{
Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, d.ToString(dtfi.ShortDatePattern, dtfi)), _elementToCleanUp.Text);
Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, d.ToString(dtfi.ShortDatePattern, dtfi)), _elementToCleanUp.TestHook.DatePickerWatermarkedTextBox.Text);
_elementToCleanUp.SelectedDate = null;
});
EnqueueCallback(delegate
{
Assert.AreEqual(_elementToCleanUp.TestHook.DatePickerWatermarkedTextBox.Watermark, string.Format(CultureInfo.CurrentCulture, Resource.DatePicker_WatermarkText, dtfi.ShortDatePattern.ToString()));
Assert.IsTrue(string.IsNullOrEmpty(_elementToCleanUp.Text));
Assert.IsTrue(string.IsNullOrEmpty(_elementToCleanUp.TestHook.DatePickerWatermarkedTextBox.Text));
_isLoaded = false;
});
EnqueueTestComplete();
}
示例9: Generate
/// <summary>
/// Generates SQL to specify a constant DateTime default value being set on a column.
/// This method just generates the actual value, not the SQL to set the default value.
/// </summary>
/// <param name="defaultValue"> The value to be set. </param>
/// <returns> SQL representing the default value. </returns>
protected virtual string Generate(DateTime defaultValue)
{
return "'" + defaultValue.ToString(DateTimeFormat, CultureInfo.InvariantCulture) + "'";
}
示例10: FormatHttpCookieDateTime
internal static String FormatHttpCookieDateTime(DateTime dt) {
if (dt < DateTime.MaxValue.AddDays(-1) && dt > DateTime.MinValue.AddDays(1))
dt = dt.ToUniversalTime();
return dt.ToString("ddd, dd-MMM-yyyy HH':'mm':'ss 'GMT'", DateTimeFormatInfo.InvariantInfo);
}
示例11: PrintTime
string PrintTime(DateTime t) {
return t.ToString("T", DateTimeFormatInfo.InvariantInfo) + "." + t.Millisecond.ToString("d03", CultureInfo.InvariantCulture);
}
示例12: ParseDate
private void ParseDate( string input )
{
int digits = DigitsInText( input );
input = input.Replace( "\\", "" ).Replace( "\"", "" );
if ( digits == 0 ){
_data = null;
_str = "-";
return;
}
else if ( digits >= 6 )
{
int firstSlash = input.IndexOf( "/" ), secondSlash = input.IndexOf( "/", firstSlash + 1 );
int month = Int16.Parse( input.Substring( 0, firstSlash ) ), day = Int16.Parse( input.Substring( firstSlash + 1, secondSlash - ( firstSlash + 1) ) ), year = Int16.Parse( input.Substring( secondSlash + 1 ));
_data = new DateTime( year, month, day );
_str = ( (DateTime)_data ).ToShortDateString( );
return;
}
else if ( DashesInText(input) == 2 )
{
int dashOne = input.IndexOf( "-" ), dashTwo = input.IndexOf( "-", dashOne + 1 );
int day = Int16.Parse( input.Substring( 0, dashOne ) ), month = MonthMap [ input.Substring( dashOne + 1, 3 ) ], year = Int16.Parse( input.Substring( dashTwo ) ) + 2000;
_data = new DateTime( year, month, day );
_str = _data.ToString( );
}
else
{
if ( input.Contains( "pm" ) || input.Contains( "am" ) )
{
int endIndex = input.Contains( "pm" ) ? input.IndexOf( "pm" ) : input.IndexOf( "am" );
string tempSubstring = input.Substring( 0, endIndex );
int colonIndex = tempSubstring.IndexOf( ":" );
int hour = Int32.Parse( tempSubstring.Substring( 0, colonIndex ).Replace( "-", "" ) ), minutes = Int32.Parse( tempSubstring.Substring( colonIndex + 1 ).Replace( "-", "" ) );
// Add time of day
DateTime dt = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minutes, 0 );
_data = dt;
_str = dt.Hour.ToString( ) + ":" + (dt.Minute.ToString( ).Length == 2 ? dt.Minute.ToString( ) : "0" + dt.Minute.ToString( ));
return;
}
else
{
int spaceIndex = input.IndexOf( " " );
int month = MonthMap [ input.Substring( 0, spaceIndex ) ], day = Int16.Parse( input.Substring( spaceIndex + 1 ) );
_data = new DateTime( DateTime.Now.Year, month, day );
_str = ( (DateTime)_data ).ToShortDateString( );
return;
}
}
}
示例13: DatePicker_AutomationPeer
[Description("Tests the creation of an automation peer for the DatePicker")]
public void DatePicker_AutomationPeer()
{
_elementToCleanUp = new DatePicker();
_isLoaded = false;
DatePickerAutomationPeer datePickerAutomationPeer = (DatePickerAutomationPeer)DatePickerAutomationPeer.CreatePeerForElement(_elementToCleanUp);
Assert.IsNotNull(datePickerAutomationPeer);
DateTime date = new DateTime(2000, 2, 2);
_elementToCleanUp.SelectedDate = date;
_elementToCleanUp.Height = 30;
_elementToCleanUp.Width = 100;
_elementToCleanUp.Loaded += new RoutedEventHandler(_elementToCleanUp_Loaded);
TestPanel.Children.Add(_elementToCleanUp);
EnqueueConditional(IsLoaded);
DateTime date2 = new DateTime(2000, 5, 5);
EnqueueCallback(delegate
{
Assert.AreEqual(datePickerAutomationPeer.GetAutomationControlType(), AutomationControlType.ComboBox, "Incorrect Control type for datepicker");
Assert.AreEqual(datePickerAutomationPeer.GetClassName(), _elementToCleanUp.GetType().Name, "Incorrect ClassName value for datepicker");
Assert.AreEqual(datePickerAutomationPeer.GetName(), date.ToString(), "Incorrect Name value for datepickerpeer");
Assert.IsTrue(datePickerAutomationPeer.IsContentElement(), "Incorrect IsContentElement value");
Assert.IsTrue(datePickerAutomationPeer.IsControlElement(), "Incorrect IsControlElement value");
#region DatePickerAutomationPeer IExpandCollapseProvider tests:
IExpandCollapseProvider datePickerExpandCollapseProvider = ((IExpandCollapseProvider)datePickerAutomationPeer.GetPattern(PatternInterface.ExpandCollapse));
Assert.IsNotNull(datePickerAutomationPeer);
Assert.AreEqual(datePickerExpandCollapseProvider.ExpandCollapseState, ExpandCollapseState.Collapsed);
_elementToCleanUp.IsDropDownOpen = true;
Assert.IsTrue(_elementToCleanUp.TestHook.DropDown.IsOpen);
Assert.AreEqual(datePickerExpandCollapseProvider.ExpandCollapseState, ExpandCollapseState.Expanded);
datePickerExpandCollapseProvider.Collapse();
Assert.AreEqual(datePickerExpandCollapseProvider.ExpandCollapseState, ExpandCollapseState.Collapsed);
Assert.IsFalse(_elementToCleanUp.TestHook.DropDown.IsOpen);
datePickerExpandCollapseProvider.Expand();
Assert.IsTrue(_elementToCleanUp.TestHook.DropDown.IsOpen);
Assert.AreEqual(datePickerExpandCollapseProvider.ExpandCollapseState, ExpandCollapseState.Expanded);
datePickerExpandCollapseProvider.Collapse();
#endregion
#region DatePickerAutomationPeer IValueProvider tests:
IValueProvider datePickerValueProvider = ((IValueProvider)datePickerAutomationPeer.GetPattern(PatternInterface.Value));
Assert.IsNotNull(datePickerValueProvider);
Assert.IsFalse(datePickerValueProvider.IsReadOnly);
Assert.AreEqual(datePickerValueProvider.Value, date.ToString());
_elementToCleanUp.SelectedDate = null;
Assert.AreEqual(datePickerValueProvider.Value, string.Empty);
datePickerValueProvider.SetValue(date2.ToString());
Assert.AreEqual(_elementToCleanUp.SelectedDate, date2);
Assert.AreEqual(datePickerValueProvider.Value, date2.ToString());
#endregion
});
EnqueueSleep(sleepTime);
EnqueueCallback(delegate
{
Assert.AreEqual(_elementToCleanUp.Text, date2.ToString("d"));
});
EnqueueTestComplete();
示例14: SendText
private void SendText()
{
DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
DateTime d = new DateTime(2003, 10, 10);
_elementToCleanUp.Text = d.ToString("d");
_elementToCleanUp.TestHook.SetSelectedDate();
Assert.AreEqual(_elementToCleanUp.SelectedDate, new DateTime(2003, 10, 10));
d = new DateTime(2003, 12, 10);
_elementToCleanUp.Text = d.ToString("D");
_elementToCleanUp.TestHook.SetSelectedDate();
Assert.AreEqual(_elementToCleanUp.SelectedDate, new DateTime(2003, 12, 10));
d = new DateTime(2003, 11, 10);
_elementToCleanUp.Text = d.ToString("G");
_elementToCleanUp.TestHook.SetSelectedDate();
Assert.AreEqual(_elementToCleanUp.SelectedDate, new DateTime(2003, 11, 10));
_isLoaded = false;
示例15: VerifyValidTextBeforeLoaded
private void VerifyValidTextBeforeLoaded()
{
DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
DateTime date = new DateTime(2005, 5, 5);
Assert.AreEqual(_elementToCleanUp.Text, string.Format(CultureInfo.CurrentCulture, date.ToString(dtfi.ShortDatePattern, dtfi)));
_isLoaded = false;