当前位置: 首页>>代码示例>>C#>>正文


C# DateTime结构体代码示例

本文整理汇总了C#中System.DateTime结构体的典型用法代码示例。如果您正苦于以下问题:C# DateTime结构体的具体用法?C# DateTime怎么用?C# DateTime使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。


DateTime结构体属于System命名空间,在下文中一共展示了DateTime结构体的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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...


注:本文中的System.DateTime结构体示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。