本文整理汇总了C#中System.DateTime结构体的典型用法代码示例。如果您正苦于以下问题:C# DateTime结构体的具体用法?C# DateTime怎么用?C# DateTime使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
DateTime结构体属于System命名空间,在下文中一共展示了DateTime结构体的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1:
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;
DateTime date3 = DateTime.Today;
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:DateTime
示例2:
var dateString = "5/1/2008 8:30:52 AM";
DateTime date1 = DateTime.Parse(dateString,
System.Globalization.CultureInfo.InvariantCulture);
var iso8601String = "20080501T08:30:52Z";
DateTime dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ",
System.Globalization.CultureInfo.InvariantCulture);
开发者ID:.NET开发者,项目名称:System,代码行数:6,代码来源:DateTime
示例3: DateTime
var date1 = new DateTime(2008, 3, 1, 7, 0, 0);
Console.WriteLine(date1.ToString());
// For en-US culture, displays 3/1/2008 7:00:00 AM
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:DateTime
示例4: DateTime
var date1 = new DateTime(2008, 3, 1, 7, 0, 0);
Console.WriteLine(date1.ToString("F"));
// Displays Saturday, March 01, 2008 7:00:00 AM
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:DateTime
示例5: foreach
string[] formats = { "yyyyMMdd", "HHmmss" };
string[] dateStrings = { "20130816", "20131608", " 20130816 ",
"115216", "521116", " 115216 " };
DateTime parsedDate;
foreach (var dateString in dateStrings)
{
if (DateTime.TryParseExact(dateString, formats, null,
System.Globalization.DateTimeStyles.AllowWhiteSpaces |
System.Globalization.DateTimeStyles.AdjustToUniversal,
out parsedDate))
Console.WriteLine($"{dateString} --> {parsedDate:g}");
else
Console.WriteLine($"Cannot convert {dateString}");
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:DateTime 输出:
20130816 --> 8/16/2013 12:00 AM
Cannot convert 20131608
20130816 --> 8/16/2013 12:00 AM
115216 --> 4/22/2013 11:52 AM
Cannot convert 521116
115216 --> 4/22/2013 11:52 AM
示例6: DateTime
var thTH = new System.Globalization.CultureInfo("th-TH");
var value = new DateTime(2016, 5, 28);
Console.WriteLine(value.ToString(thTH));
thTH.DateTimeFormat.Calendar = new System.Globalization.GregorianCalendar();
Console.WriteLine(value.ToString(thTH));
开发者ID:.NET开发者,项目名称:System,代码行数:7,代码来源:DateTime 输出:
28/5/2559 0:00:00
28/5/2016 0:00:00
示例7:
var thTH = new System.Globalization.CultureInfo("th-TH");
var value = DateTime.Parse("28/05/2559", thTH);
Console.WriteLine(value.ToString(thTH));
thTH.DateTimeFormat.Calendar = new System.Globalization.GregorianCalendar();
Console.WriteLine(value.ToString(thTH));
开发者ID:.NET开发者,项目名称:System,代码行数:6,代码来源:DateTime 输出:
28/5/2559 0:00:00
28/5/2016 0:00:00
示例8: DateTime
var thTH = new System.Globalization.CultureInfo("th-TH");
var dat = new DateTime(2559, 5, 28, thTH.DateTimeFormat.Calendar);
Console.WriteLine($"Thai Buddhist era date: {dat.ToString("d", thTH)}");
Console.WriteLine($"Gregorian date: {dat:d}");
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:DateTime 输出:
Thai Buddhist Era Date: 28/5/2559
Gregorian Date: 28/05/2016
示例9: DateTime
var thTH = new System.Globalization.CultureInfo("th-TH");
var cal = thTH.DateTimeFormat.Calendar;
var dat = new DateTime(2559, 5, 28, cal);
Console.WriteLine("Using the Thai Buddhist Era calendar:");
Console.WriteLine($"Date: {dat.ToString("d", thTH)}");
Console.WriteLine($"Year: {cal.GetYear(dat)}");
Console.WriteLine($"Leap year: {cal.IsLeapYear(cal.GetYear(dat))}\n");
Console.WriteLine("Using the Gregorian calendar:");
Console.WriteLine($"Date: {dat:d}");
Console.WriteLine($"Year: {dat.Year}");
Console.WriteLine($"Leap year: {DateTime.IsLeapYear(dat.Year)}");
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:DateTime 输出:
Using the Thai Buddhist Era calendar
Date : 28/5/2559
Year: 2559
Leap year : True
Using the Gregorian calendar
Date : 28/05/2016
Year: 2016
Leap year : True
示例10: DateTime
var thTH = new System.Globalization.CultureInfo("th-TH");
var thCalendar = thTH.DateTimeFormat.Calendar;
var dat = new DateTime(1395, 8, 18, thCalendar);
Console.WriteLine("Using the Thai Buddhist Era calendar:");
Console.WriteLine($"Date: {dat.ToString("d", thTH)}");
Console.WriteLine($"Day of Week: {thCalendar.GetDayOfWeek(dat)}");
Console.WriteLine($"Week of year: {thCalendar.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}\n");
var greg = new System.Globalization.GregorianCalendar();
Console.WriteLine("Using the Gregorian calendar:");
Console.WriteLine($"Date: {dat:d}");
Console.WriteLine($"Day of Week: {dat.DayOfWeek}");
Console.WriteLine($"Week of year: {greg.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Sunday)}");
开发者ID:.NET开发者,项目名称:System,代码行数:13,代码来源:DateTime 输出:
Using the Thai Buddhist Era calendar
Date : 18/8/1395
Day of Week: Sunday
Week of year: 34
Using the Gregorian calendar
Date : 18/08/0852
Day of Week: Sunday
Week of year: 34
示例11: PersistAsLocalStrings
public static void PersistAsLocalStrings()
{
SaveLocalDatesAsString();
RestoreLocalDatesFromString();
}
private static void SaveLocalDatesAsString()
{
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
new DateTime(2014, 7, 10, 23, 49, 0),
new DateTime(2015, 1, 10, 1, 16, 0),
new DateTime(2014, 12, 20, 21, 45, 0),
new DateTime(2014, 6, 2, 15, 14, 0) };
string output = null;
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
for (int ctr = 0; ctr < dates.Length; ctr++)
{
Console.WriteLine(dates[ctr].ToString("f"));
output += dates[ctr].ToString() + (ctr != dates.Length - 1 ? "|" : "");
}
var sw = new StreamWriter(filenameTxt);
sw.Write(output);
sw.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreLocalDatesFromString()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
StreamReader sr = new StreamReader(filenameTxt);
string[] inputValues = sr.ReadToEnd().Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries);
sr.Close();
Console.WriteLine("The dates on an {0} system:",
Thread.CurrentThread.CurrentCulture.Name);
foreach (var inputValue in inputValues)
{
DateTime dateValue;
if (DateTime.TryParse(inputValue, out dateValue))
{
Console.WriteLine($"'{inputValue}' --> {dateValue:f}");
}
else
{
Console.WriteLine("Cannot parse '{inputValue}'");
}
}
Console.WriteLine("Restored dates...");
}
开发者ID:.NET开发者,项目名称:System,代码行数:53,代码来源:DateTime 输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
The dates on an en-US system:
Saturday, June 14, 2014 6:32 AM
Thursday, July 10, 2014 11:49 PM
Saturday, January 10, 2015 1:16 AM
Saturday, December 20, 2014 9:45 PM
Monday, June 02, 2014 3:14 PM
Saved dates...
When restored on an en-GB system, the example displays the following output:
Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
The dates on an en-GB system:
Cannot parse //6/14/2014 6:32:00 AM//
7/10/2014 11:49:00 PM// --> 07 October 2014 23:49
1/10/2015 1:16:00 AM// --> 01 October 2015 01:16
Cannot parse //12/20/2014 9:45:00 PM//
6/2/2014 3:14:00 PM// --> 06 February 2014 15:14
Restored dates...
示例12: PersistAsInvariantStrings
public static void PersistAsInvariantStrings()
{
SaveDatesAsInvariantStrings();
RestoreDatesAsInvariantStrings();
}
private static void SaveDatesAsInvariantStrings()
{
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
new DateTime(2014, 7, 10, 23, 49, 0),
new DateTime(2015, 1, 10, 1, 16, 0),
new DateTime(2014, 12, 20, 21, 45, 0),
new DateTime(2014, 6, 2, 15, 14, 0) };
string output = null;
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
for (int ctr = 0; ctr < dates.Length; ctr++)
{
Console.WriteLine(dates[ctr].ToString("f"));
output += dates[ctr].ToUniversalTime().ToString("O", CultureInfo.InvariantCulture)
+ (ctr != dates.Length - 1 ? "|" : "");
}
var sw = new StreamWriter(filenameTxt);
sw.Write(output);
sw.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreDatesAsInvariantStrings()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine("Current Time Zone: {0}",
TimeZoneInfo.Local.DisplayName);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
StreamReader sr = new StreamReader(filenameTxt);
string[] inputValues = sr.ReadToEnd().Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries);
sr.Close();
Console.WriteLine("The dates on an {0} system:",
Thread.CurrentThread.CurrentCulture.Name);
foreach (var inputValue in inputValues)
{
DateTime dateValue;
if (DateTime.TryParseExact(inputValue, "O", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out dateValue))
{
Console.WriteLine($"'{inputValue}' --> {dateValue.ToLocalTime():f}");
}
else
{
Console.WriteLine("Cannot parse '{0}'", inputValue);
}
}
Console.WriteLine("Restored dates...");
}
开发者ID:.NET开发者,项目名称:System,代码行数:56,代码来源:DateTime 输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
The dates on an en-US system:
Saturday, June 14, 2014 6:32 AM
Thursday, July 10, 2014 11:49 PM
Saturday, January 10, 2015 1:16 AM
Saturday, December 20, 2014 9:45 PM
Monday, June 02, 2014 3:14 PM
Saved dates...
When restored on an en-GB system, the example displays the following output:
Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
The dates on an en-GB system:
'2014-06-14T13:32:00.0000000Z' --> 14 June 2014 14:32
'2014-07-11T06:49:00.0000000Z' --> 11 July 2014 07:49
'2015-01-10T09:16:00.0000000Z' --> 10 January 2015 09:16
'2014-12-21T05:45:00.0000000Z' --> 21 December 2014 05:45
'2014-06-02T22:14:00.0000000Z' --> 02 June 2014 23:14
Restored dates...
示例13: PersistAsIntegers
public static void PersistAsIntegers()
{
SaveDatesAsInts();
RestoreDatesAsInts();
}
private static void SaveDatesAsInts()
{
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
new DateTime(2014, 7, 10, 23, 49, 0),
new DateTime(2015, 1, 10, 1, 16, 0),
new DateTime(2014, 12, 20, 21, 45, 0),
new DateTime(2014, 6, 2, 15, 14, 0) };
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
var ticks = new long[dates.Length];
for (int ctr = 0; ctr < dates.Length; ctr++)
{
Console.WriteLine(dates[ctr].ToString("f"));
ticks[ctr] = dates[ctr].ToUniversalTime().Ticks;
}
var fs = new FileStream(filenameInts, FileMode.Create);
var bw = new BinaryWriter(fs);
bw.Write(ticks.Length);
foreach (var tick in ticks)
bw.Write(tick);
bw.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreDatesAsInts()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
FileStream fs = new FileStream(filenameInts, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int items;
DateTime[] dates;
try
{
items = br.ReadInt32();
dates = new DateTime[items];
for (int ctr = 0; ctr < items; ctr++)
{
long ticks = br.ReadInt64();
dates[ctr] = new DateTime(ticks).ToLocalTime();
}
}
catch (EndOfStreamException)
{
Console.WriteLine("File corruption detected. Unable to restore data...");
return;
}
catch (IOException)
{
Console.WriteLine("Unspecified I/O error. Unable to restore data...");
return;
}
// Thrown during array initialization.
catch (OutOfMemoryException)
{
Console.WriteLine("File corruption detected. Unable to restore data...");
return;
}
finally
{
br.Close();
}
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
foreach (var value in dates)
Console.WriteLine(value.ToString("f"));
Console.WriteLine("Restored dates...");
}
开发者ID:.NET开发者,项目名称:System,代码行数:80,代码来源:DateTime 输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
The dates on an en-US system:
Saturday, June 14, 2014 6:32 AM
Thursday, July 10, 2014 11:49 PM
Saturday, January 10, 2015 1:16 AM
Saturday, December 20, 2014 9:45 PM
Monday, June 02, 2014 3:14 PM
Saved dates...
When restored on an en-GB system, the example displays the following output:
Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
The dates on an en-GB system:
14 June 2014 14:32
11 July 2014 07:49
10 January 2015 09:16
21 December 2014 05:45
02 June 2014 23:14
Restored dates...
示例14: PersistAsXML
public static void PersistAsXML()
{
// Serialize the data.
var leapYears = new List<DateTime>();
for (int year = 2000; year <= 2100; year += 4)
{
if (DateTime.IsLeapYear(year))
leapYears.Add(new DateTime(year, 2, 29));
}
DateTime[] dateArray = leapYears.ToArray();
var serializer = new XmlSerializer(dateArray.GetType());
TextWriter sw = new StreamWriter(filenameXml);
try
{
serializer.Serialize(sw, dateArray);
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.InnerException.Message);
}
finally
{
if (sw != null) sw.Close();
}
// Deserialize the data.
DateTime[] deserializedDates;
using (var fs = new FileStream(filenameXml, FileMode.Open))
{
deserializedDates = (DateTime[])serializer.Deserialize(fs);
}
// Display the dates.
Console.WriteLine($"Leap year days from 2000-2100 on an {Thread.CurrentThread.CurrentCulture.Name} system:");
int nItems = 0;
foreach (var dat in deserializedDates)
{
Console.Write($" {dat:d} ");
nItems++;
if (nItems % 5 == 0)
Console.WriteLine();
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:45,代码来源:DateTime 输出:
Leap year days from 2000-2100 on an en-GB system:
29/02/2000 29/02/2004 29/02/2008 29/02/2012 29/02/2016
29/02/2020 29/02/2024 29/02/2028 29/02/2032 29/02/2036
29/02/2040 29/02/2044 29/02/2048 29/02/2052 29/02/2056
29/02/2060 29/02/2064 29/02/2068 29/02/2072 29/02/2076
29/02/2080 29/02/2084 29/02/2088 29/02/2092 29/02/2096
示例15: PersistBinary
public static void PersistBinary()
{
SaveDatesBinary();
RestoreDatesBinary();
}
private static void SaveDatesBinary()
{
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
new DateTime(2014, 7, 10, 23, 49, 0),
new DateTime(2015, 1, 10, 1, 16, 0),
new DateTime(2014, 12, 20, 21, 45, 0),
new DateTime(2014, 6, 2, 15, 14, 0) };
var fs = new FileStream(filenameBin, FileMode.Create);
var bin = new BinaryFormatter();
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
for (int ctr = 0; ctr < dates.Length; ctr++)
{
Console.WriteLine(dates[ctr].ToString("f"));
dates[ctr] = dates[ctr].ToUniversalTime();
}
bin.Serialize(fs, dates);
fs.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreDatesBinary()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
FileStream fs = new FileStream(filenameBin, FileMode.Open);
BinaryFormatter bin = new BinaryFormatter();
var dates = (DateTime[])bin.Deserialize(fs);
fs.Close();
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
foreach (var value in dates)
Console.WriteLine(value.ToLocalTime().ToString("f"));
Console.WriteLine("Restored dates...");
}
开发者ID:.NET开发者,项目名称:System,代码行数:45,代码来源:DateTime 输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
The dates on an en-US system:
Saturday, June 14, 2014 6:32 AM
Thursday, July 10, 2014 11:49 PM
Saturday, January 10, 2015 1:16 AM
Saturday, December 20, 2014 9:45 PM
Monday, June 02, 2014 3:14 PM
Saved dates...
When restored on an en-GB system, the example displays the following output:
Current Time Zone: (UTC-6:00) Central Time (US & Canada)
The dates on an en-GB system:
14 June 2014 08:32
11 July 2014 01:49
10 January 2015 03:16
20 December 2014 23:45
02 June 2014 17:14
Restored dates...
示例16: DateWithTimeZone
//引入命名空间
using System;
namespace DateTimeExtensions
{
[Serializable]
public struct DateWithTimeZone
{
private TimeZoneInfo tz;
private DateTime dt;
public DateWithTimeZone(DateTime dateValue, TimeZoneInfo timeZone)
{
dt = dateValue;
tz = timeZone ?? TimeZoneInfo.Local;
}
public TimeZoneInfo TimeZone
{
get { return (tz); }
set { tz = value; }
}
public DateTime DateTime
{
get { return (dt); }
set { dt = value; }
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:30,代码来源:DateTime
示例17: SaveDateWithTimeZone
public static void SaveDateWithTimeZone()
{
DateWithTimeZone[] dates = { new DateWithTimeZone(new DateTime(2014, 8, 9, 19, 30, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
new DateWithTimeZone(new DateTime(2014, 8, 15, 19, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")),
new DateWithTimeZone(new DateTime(2014, 8, 22, 19, 30, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
new DateWithTimeZone(new DateTime(2014, 8, 28, 19, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")) };
var fs = new FileStream(@".\Schedule.bin", FileMode.Create);
var formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, dates);
// Display dates.
foreach (var date in dates)
{
TimeZoneInfo tz = date.TimeZone;
Console.WriteLine($"{date.DateTime} {(tz.IsDaylightSavingTime(date.DateTime) ? tz.DaylightName : tz.StandardName)}");
}
}
catch (SerializationException e)
{
Console.WriteLine($"Serialization failed. Reason: {e.Message}");
}
finally
{
if (fs != null) fs.Close();
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:31,代码来源:DateTime 输出:
8/9/2014 7:30:00 PM Eastern Daylight Time
8/15/2014 7:00:00 PM Pacific Daylight Time
8/22/2014 7:30:00 PM Eastern Daylight Time
8/28/2014 7:00:00 PM Eastern Daylight Time
示例18: RestoreDateWithTimeZone
public static void RestoreDateWithTimeZone()
{
const string filename = @".\Schedule.bin";
FileStream fs;
if (File.Exists(filename))
{
fs = new FileStream(filename, FileMode.Open);
}
else
{
Console.WriteLine("Unable to find file to deserialize.");
return;
}
var formatter = new BinaryFormatter();
DateWithTimeZone[] dates;
try
{
dates = (DateWithTimeZone[])formatter.Deserialize(fs);
// Display dates.
foreach (var date in dates)
{
TimeZoneInfo tz = date.TimeZone;
Console.WriteLine($"{ date.DateTime} {(tz.IsDaylightSavingTime(date.DateTime) ? tz.DaylightName : tz.StandardName)}");
}
}
catch (SerializationException e)
{
Console.WriteLine($"Deserialization failed. Reason: {e.Message}");
}
finally
{
if (fs != null) fs.Close();
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:35,代码来源:DateTime 输出:
8/9/2014 7:30:00 PM Eastern Daylight Time
8/15/2014 7:00:00 PM Pacific Daylight Time
8/22/2014 7:30:00 PM Eastern Daylight Time
8/28/2014 7:00:00 PM Eastern Daylight Time
示例19: RoughlyEquals
public static bool RoughlyEquals(DateTime time, DateTime timeWithWindow, int windowInSeconds, int frequencyInSeconds)
{
long delta = (long)((TimeSpan)(timeWithWindow - time)).TotalSeconds % frequencyInSeconds;
delta = delta > windowInSeconds ? frequencyInSeconds - delta : delta;
return Math.Abs(delta) < windowInSeconds;
}
public static void TestRoughlyEquals()
{
int window = 10;
int freq = 60 * 60 * 2; // 2 hours;
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddSeconds(2 * window);
DateTime d3 = d1.AddSeconds(-2 * window);
DateTime d4 = d1.AddSeconds(window / 2);
DateTime d5 = d1.AddSeconds(-window / 2);
DateTime d6 = (d1.AddHours(2)).AddSeconds(2 * window);
DateTime d7 = (d1.AddHours(2)).AddSeconds(-2 * window);
DateTime d8 = (d1.AddHours(2)).AddSeconds(window / 2);
DateTime d9 = (d1.AddHours(2)).AddSeconds(-window / 2);
Console.WriteLine($"d1 ({d1}) ~= d1 ({d1}): {RoughlyEquals(d1, d1, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d2 ({d2}): {RoughlyEquals(d1, d2, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d3 ({d3}): {RoughlyEquals(d1, d3, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d4 ({d4}): {RoughlyEquals(d1, d4, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d5 ({d5}): {RoughlyEquals(d1, d5, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d6 ({d6}): {RoughlyEquals(d1, d6, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d7 ({d7}): {RoughlyEquals(d1, d7, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d8 ({d8}): {RoughlyEquals(d1, d8, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d9 ({d9}): {RoughlyEquals(d1, d9, window, freq)}");
}
开发者ID:.NET开发者,项目名称:System,代码行数:35,代码来源:DateTime 输出:
d1 (1/28/2010 9:01:26 PM) ~= d1 (1/28/2010 9:01:26 PM): True
d1 (1/28/2010 9:01:26 PM) ~= d2 (1/28/2010 9:01:46 PM): False
d1 (1/28/2010 9:01:26 PM) ~= d3 (1/28/2010 9:01:06 PM): False
d1 (1/28/2010 9:01:26 PM) ~= d4 (1/28/2010 9:01:31 PM): True
d1 (1/28/2010 9:01:26 PM) ~= d5 (1/28/2010 9:01:21 PM): True
d1 (1/28/2010 9:01:26 PM) ~= d6 (1/28/2010 11:01:46 PM): False
d1 (1/28/2010 9:01:26 PM) ~= d7 (1/28/2010 11:01:06 PM): False
d1 (1/28/2010 9:01:26 PM) ~= d8 (1/28/2010 11:01:31 PM): True
d1 (1/28/2010 9:01:26 PM) ~= d9 (1/28/2010 11:01:21 PM): True
示例20: new DateTime
//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MainClass
{
public static void Main()
{
DateTime dt1 = new DateTime(2004, 10, 19);
Console.WriteLine("dt1: {0}", dt1);
DateTime dt2 = new DateTime(2004, 10, 19, 22, 47, 35);
Console.WriteLine("dt2: {0}", dt2);
DateTime dt3 = new DateTime(2004, 10, 19, 22, 47, 35, 259);
Console.WriteLine("dt3: {0}", dt3);
}
}
开发者ID:C#程序员,项目名称:System,代码行数:20,代码来源:DateTime
注:本文中的System.DateTime结构体示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论