Die Anleitung zu C# Date Time

1- Die Klasse über Date, Time im C#

Im .NET Framework ist  System.DateTime eine Klasse zur Vertretung des Datum und der Zeit. Ihre Wert liegt im Raum vom12:00:00 nacht am 01-01-0001 bis 11:59:59 abend am 31-12-9999.
Es gibt viele Constructor, damit Sie ein Objekt  DateTime.erstellen
DateTime Constructors
public DateTime(int year, int month, int day)

public DateTime(int year, int month, int day, Calendar calendar)

public DateTime(int year, int month, int day, int hour, int minute, int second)

public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar)

public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)

public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)

public DateTime(int year, int month, int day, int hour,
                    int minute, int second, int millisecond, Calendar calendar)

public DateTime(int year, int month, int day, int hour,
                   int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind)

public DateTime(int year, int month, int day, int hour,
                    int minute, int second, int millisecond, DateTimeKind kind)

public DateTime(long ticks)

public DateTime(long ticks, DateTimeKind kind)
  • TODO - EXAMPLE
Now ist eine static property von DateTime ,sie gibt das Objekt  DateTime mit der momentanen Bezeichnung rück
// The object describes the current time.
DateTime now = DateTime.Now;

Console.WriteLine("Now is "+ now);

2- Die Property von DateTime

Property Data Type Description
Date DateTime

Gets the date component of this instance.

Day int

Gets the day of the month represented by this instance.

DayOfWeek DayOfWeek

Gets the day of the week represented by this instance.

DayOfYear int

Gets the day of the year represented by this instance.

Hour int

Gets the hour component of the date represented by this instance.

Kind DateTimeKind

Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.

Millisecond int

Gets the milliseconds component of the date represented by this instance.

Minute int

Gets the minute component of the date represented by this instance.

Month int

Gets the month component of the date represented by this instance.

Now DateTime

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.

Second int

Gets the seconds component of the date represented by this instance.

Ticks long

Gets the number of ticks that represent the date and time of this instance.
(1 Minute = 600 million ticks).

TimeOfDay TimeSpan

Gets the time of day for this instance.

Today DateTime

Gets the current date.

UtcNow DateTime

Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).

Year int

Gets the year component of the date represented by this instance.

DateTimePropertiesExample.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class DateTimePropertiesExample
    {

        public static void Main(string[] args)
        {
            // Create a DateTime (year, month, day, hour, minute, second).
            DateTime aDateTime = new DateTime(2005, 11, 20, 12, 1, 10); 

            // Print out infos:
            Console.WriteLine("Day:{0}", aDateTime.Day);
            Console.WriteLine("Month:{0}", aDateTime.Month);
            Console.WriteLine("Year:{0}", aDateTime.Year);
            Console.WriteLine("Hour:{0}", aDateTime.Hour);
            Console.WriteLine("Minute:{0}", aDateTime.Minute);
            Console.WriteLine("Second:{0}", aDateTime.Second);
            Console.WriteLine("Millisecond:{0}", aDateTime.Millisecond);

            // Enum {Monday, Tuesday,... Sunday}
            DayOfWeek dayOfWeek = aDateTime.DayOfWeek;   


            Console.WriteLine("Day of Week:{0}", dayOfWeek ); 
           

            Console.WriteLine("Day of Year: {0}", aDateTime.DayOfYear);

            // An object described only time (hour minute, ..)
            TimeSpan timeOfDay = aDateTime.TimeOfDay; 

            Console.WriteLine("Time of Day:{0}", timeOfDay);

            // Convert to Ticks (1 second= 10.000.000 Ticks)
            Console.WriteLine("Tick:{0}", aDateTime.Ticks);

            // {Local, Itc, Unspecified}
            DateTimeKind kind = aDateTime.Kind; 

            Console.WriteLine("Kind:{0}", kind);

            Console.Read();
        }
    }

}
Das Beispiel durchführen

3- Die Zeit einfügen und vermindern

DateTime bietet die Methode, damit Sie ein Zeitraum ergänzen oder abziehen können. TimeSpan ist eine Klasse zur Enthaltung einer zeitlichen Information. Es kann als einen Parameter in der Methode zur Ergänzung oder Abziehung der Zeit vom DateTime funktionieren
Zum Beispiel
AddSubtractExample.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class AddSubtractExample
    {

        public static void Main(string[] args)
        {
            // Current Time
            DateTime aDateTime = DateTime.Now;

            Console.WriteLine("Now is " + aDateTime);

            // An Interval.
            // 1 hour + 1 minute
            TimeSpan aInterval = new System.TimeSpan(0, 1, 1, 0);

            // Add an interval.
            DateTime newTime = aDateTime.Add(aInterval);


            Console.WriteLine("After add 1 hour, 1 minute: " + newTime);

            // Subtract an interval.
            newTime = aDateTime.Subtract(aInterval);

            Console.WriteLine("After subtract 1 hour, 1 minute: " + newTime);

            Console.Read();
        }
    }

}
Das Beispiel durchführen
Die Klasse  DateTime hat auch die Methode zur Ergänzung/Abziehung einer zeitlichen Einheit wie
  • AddYears
  • AddDays
  • AddMinutes
  • ...
Zum Beispiel
AddSubtractExample2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class AddSubtractExample2
    {
        public static void Main(string[] args)
        {
            // The current time.
            DateTime aDateTime = DateTime.Now;

            Console.WriteLine("Now is " + aDateTime);           

            // Add one year.
            DateTime newTime = aDateTime.AddYears(1);


            Console.WriteLine("After add 1 year: " + newTime);

            // Subtract 1 hour
            newTime = aDateTime.AddHours(-1);

            Console.WriteLine("After add -1 hour: " + newTime);

            Console.Read();
        }
    }

}
Das Beispiel durchführen
Manchmals sollen Sie den ersten oder letzten Tag eines bestimmten Monat oder Jahres finden. Zum Beispiel stellen Sie eine Frage: Was ist der letzte Tag von Februar des Jahres 2015? 28. oder 29. Tag. Das folgende Beispiel beschreibt einige Methode zur Anwort der Frage
FirstLastDayDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class FirstLastDayDemo
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Today is " + DateTime.Today);

            DateTime yesterday = GetYesterday();

            Console.WriteLine("Yesterday is " + yesterday);

            // The first day of February 2015
            DateTime aDateTime = GetFistDayInMonth(2015, 2);

            Console.WriteLine("First day of 2-2015: " + aDateTime);

            // The last day of February 2015
            aDateTime = GetLastDayInMonth(2015, 2);

            Console.WriteLine("Last day of 2-2015: " + aDateTime);

            // The first day of 2015
            aDateTime = GetFirstDayInYear(2015);

            Console.WriteLine("First day year 2015: " + aDateTime);

            // The last day of 2015
            aDateTime = GetLastDayInYear(2015);

            Console.WriteLine("First day year 2015: " + aDateTime);

            Console.Read();
        }

        // Returns Yesterday.
        public static DateTime GetYesterday()
        {
            // Today.
            DateTime today = DateTime.Today;

            // Subtract 1 day.
            return today.AddDays(-1);
        }

        // Returns the first day of the year
        public static DateTime GetFirstDayInYear(int year)
        {
            DateTime aDateTime = new DateTime(year, 1, 1);
            return aDateTime;
        }

        // Returns the last day of the year
        public static DateTime GetLastDayInYear(int year)
        {
            DateTime aDateTime = new DateTime(year +1, 1, 1);

            // Subtract 1 day.
            DateTime retDateTime = aDateTime.AddDays(-1);

            return retDateTime;
        }

        // Return first day of Month.
        public static DateTime GetFistDayInMonth(int year, int month)
        {
            DateTime aDateTime = new DateTime(year, month, 1);

            return aDateTime;
        }

        // Return last day of Month.
        public static DateTime GetLastDayInMonth(int year, int month)
        {
            DateTime aDateTime = new DateTime(year, month, 1);

            // Add 1 month and substract 1 day.
            DateTime retDateTime = aDateTime.AddMonths(1).AddDays(-1);

            return retDateTime;
        }

    }

}
Das Beispiel durchführen

4- Das Zeitraum messen

Sie haben 2 Objekte   DateTime, Sie können die Zeitraum zwischen 2 Objekte kalkulieren. Das Ergebnis erhalten Sie ein Objekt  TimeSpan.
IntervalDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class IntervalDemo
    {
        public static void Main(string[] args)
        {
            // The current time.
            DateTime aDateTime = DateTime.Now;

            // Time in 2000
            DateTime y2K = new DateTime(2000,1,1);

            // Interval from 2000 until now
            TimeSpan interval = aDateTime.Subtract(y2K);


            Console.WriteLine("Interval from Y2K to Now: " + interval);

            Console.WriteLine("Days: " + interval.Days);
            Console.WriteLine("Hours: " + interval.Hours);
            Console.WriteLine("Minutes: " + interval.Minutes);
            Console.WriteLine("Seconds: " + interval.Seconds);
 

            Console.Read();
        }
    }

}
Das Beispiel durchführen

5- Der Vergleich des 2 Objekte DateTime

DateTime hat eine static Methode Compare. Die Methode ist für das Vergleich von 2 Objekt DateTime , welches Objekt ist früher als das andere:
// If return value < 0 means firstDateTime is earlier
// If return value > 0 means firstDateTime is laster
// If return value = 0 means firstDateTime and secondDateTime same time
public static int Compare(DateTime firstDateTime, DateTime secondDateTime);
CompareDateTimeExample.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class CompareDateTimeExample
    {
        public static void Main(string[] args)
        {
            // Current time.
            DateTime firstDateTime = new DateTime(2000, 9, 2);

            DateTime secondDateTime = new DateTime(2011, 1, 20);

            int compare = DateTime.Compare(firstDateTime, secondDateTime);

            Console.WriteLine("First DateTime: " + firstDateTime);
            Console.WriteLine("Second DateTime: " + secondDateTime);

            Console.WriteLine("Compare value: " + compare);// -1

            if (compare < 0)
            {
                // firstDateTime is earlier than secondDateTime.
                Console.WriteLine("firstDateTime is earlier than secondDateTime");
            }
            else
            {
                // firstDateTime is laster than secondDateTime
                Console.WriteLine("firstDateTime is laster than secondDateTime");
            }

            Console.Read();
        }
    }

}
Das Beispiel durchführen

6- Standard DateTime formatieren

Die Format  DateTime bedeutet die Umwandlung des Objekt  DateTime zu einem String nach einer VOrlage, zum Beispiel nach der Format  Tag/Monat/Jahr, ...oder nach der bestimmten lokalen Format
Die Methode  GetDateTimeFormats von  DateTime:
  • die Wert des Objekt (DateTime) zu einem Array des nach der Vorlage formatierten String umwandeln
AllStandardFormatsDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class AllStandardFormatsDemo
    {
        public static void Main(string[] args)
        {

            DateTime aDateTime = new DateTime(2015, 12, 20, 11, 30, 50);

            // Date-time supported formats.
            string[] formattedStrings = aDateTime.GetDateTimeFormats();

            foreach (string format in formattedStrings)
            {
                Console.WriteLine(format);
            }


            Console.Read();
        }
    }

}
Das Beispiel durchführen
Das oben Beispiel listiert die folgenden String nach der Formatiereung eines Objekt DateTime nach der durch.NET unterstützten Vorlage. Sie benutzen einige folgenden Methode um die Format nach der bestimmten Vorlage zu nehmen
Die Methode Die Bezeichnung
ToString(String, IFormatProvider) Die Umwandlung der Werte des Objekt DateTime zum äquivalenten Vertretungsstring durch die Benutzung der durch String gegebenen Format und die Information zur Kultursformatierung, die durch den Parameter  IFormatProvider gegeben wird
ToString(IFormatProvider)

Die Umwandlung der Werte des Objekt DateTime zum der Kultursformatierung entsprechenden String, der durch den Parameter IFormatProvider gegeben wird

ToString(String) Die Umwandlung der Werte des Objekt DateTime zu seinem äquivalenten String durch die Benutzung der durch String gegebenen Format und der Regelung der aktuellen Kultur.
Das folgende Beispiel formatiert DateTime nach der Format von 'd' und bestimmt die Kultur im Parameter
SimpleDateTimeFormat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace DateTimeTutorial
{
    class SimpleDateTimeFormat
    {
        public static void Main(string[] args)
        {

            DateTime aDateTime = new DateTime(2015, 12, 20, 11, 30, 50);

            Console.WriteLine("DateTime: " + aDateTime);

            String d_formatString = aDateTime.ToString("d");

            Console.WriteLine("Format 'd' : " + d_formatString);

            // An object described American culture (en-US Culture)
            CultureInfo enUs = new CultureInfo("en-US"); 

            // ==> 12/20/2015 (MM/dd/yyyy)
            Console.WriteLine("Format 'd' & en-US: " + aDateTime.ToString("d", enUs));

            // vietnam Culture.
            CultureInfo viVn = new CultureInfo("vi-VN");

            // ==> 12/20/2015 (dd/MM/yyyy)
            Console.WriteLine("Format 'd' & vi-VN: " + aDateTime.ToString("d", viVn));


            Console.Read();
        }
    }

}
Das Beispiel durchführen
Die standardmäßigen Formatierungszeichen 
Code Pattern
"d" Das kurze Datum
"D" Das lange Datum
"f" Das lange Datum, die kurze Zeit
"F" Das lange Datum, die lange Zeit.
"g" Das Datum im Allgemein. die kurze Zeit
"G" Das Datum im Allgemein. die lange Zeit
"M", 'm" Der Monat/Tag
"O", "o" Round-trip date/time.
"R", "r" RFC1123
"s" Das sortierbare Datum
"t" Die kurze Zeit
"T" Die lange Zeit
"u" Universal sortable date time
"U" Universal full date time
"Y", "y" Der Jahr/Monat
SimpleDateTimeFormatAll.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class SimpleDateTimeFormatAll
    {
        public static void Main(string[] args)
        {
            char[] formats = {'d', 'D','f','F','g','G','M', 'm','O', 'o','R', 'r','s','t','T','u','U','Y', 'y'};


            DateTime aDateTime = new DateTime(2015, 12, 20, 11, 30, 50);

            foreach (char ch in formats)
            {
                Console.WriteLine("\n======" + ch + " ========\n");

                // Date-time suppported Formats.
                string[] formattedStrings = aDateTime.GetDateTimeFormats(ch);

                foreach (string format in formattedStrings)
                {
                    Console.WriteLine(format);
                }
            }
          


            Console.ReadLine();
        }
    }

}
Das Beispiel durchführen

======d ========

12/20/2015
12/20/15
12/20/15
12/20/2015
15/12/20
2015-12-20
20-Dec-15

======D ========

Sunday, December 20, 2015
December 20, 2015
Sunday, 20 December, 2015
20 December, 2015

======f ========

Sunday, December 20, 2015 11:30 AM
Sunday, December 20, 2015 11:30 AM
Sunday, December 20, 2015 11:30
Sunday, December 20, 2015 11:30
December 20, 2015 11:30 AM
December 20, 2015 11:30 AM
December 20, 2015 11:30
December 20, 2015 11:30
Sunday, 20 December, 2015 11:30 AM
Sunday, 20 December, 2015 11:30 AM
Sunday, 20 December, 2015 11:30
Sunday, 20 December, 2015 11:30
20 December, 2015 11:30 AM
20 December, 2015 11:30 AM
20 December, 2015 11:30
20 December, 2015 11:30

======F ========

Sunday, December 20, 2015 11:30:50 AM
Sunday, December 20, 2015 11:30:50 AM
Sunday, December 20, 2015 11:30:50
Sunday, December 20, 2015 11:30:50
December 20, 2015 11:30:50 AM
December 20, 2015 11:30:50 AM
December 20, 2015 11:30:50
December 20, 2015 11:30:50
Sunday, 20 December, 2015 11:30:50 AM
Sunday, 20 December, 2015 11:30:50 AM
Sunday, 20 December, 2015 11:30:50
Sunday, 20 December, 2015 11:30:50
20 December, 2015 11:30:50 AM
20 December, 2015 11:30:50 AM
20 December, 2015 11:30:50
20 December, 2015 11:30:50

======g ========

12/20/2015 11:30 AM
12/20/2015 11:30 AM
12/20/2015 11:30
12/20/2015 11:30
12/20/15 11:30 AM
12/20/15 11:30 AM
12/20/15 11:30
12/20/15 11:30
12/20/15 11:30 AM
12/20/15 11:30 AM
12/20/15 11:30
12/20/15 11:30
12/20/2015 11:30 AM
12/20/2015 11:30 AM
12/20/2015 11:30
12/20/2015 11:30
15/12/20 11:30 AM
15/12/20 11:30 AM
15/12/20 11:30
15/12/20 11:30
2015-12-20 11:30 AM
2015-12-20 11:30 AM
2015-12-20 11:30
2015-12-20 11:30
20-Dec-15 11:30 AM
20-Dec-15 11:30 AM
20-Dec-15 11:30
20-Dec-15 11:30

======G ========

12/20/2015 11:30:50 AM
12/20/2015 11:30:50 AM
12/20/2015 11:30:50
12/20/2015 11:30:50
12/20/15 11:30:50 AM
12/20/15 11:30:50 AM
12/20/15 11:30:50
12/20/15 11:30:50
12/20/15 11:30:50 AM
12/20/15 11:30:50 AM
12/20/15 11:30:50
12/20/15 11:30:50
12/20/2015 11:30:50 AM
12/20/2015 11:30:50 AM
12/20/2015 11:30:50
12/20/2015 11:30:50
15/12/20 11:30:50 AM
15/12/20 11:30:50 AM
15/12/20 11:30:50
15/12/20 11:30:50
2015-12-20 11:30:50 AM
2015-12-20 11:30:50 AM
2015-12-20 11:30:50
2015-12-20 11:30:50
20-Dec-15 11:30:50 AM
20-Dec-15 11:30:50 AM
20-Dec-15 11:30:50
20-Dec-15 11:30:50

======M ========

December 20

======m ========

December 20

======O ========

2015-12-20T11:30:50.0000000

======o ========

2015-12-20T11:30:50.0000000

======R ========

Sun, 20 Dec 2015 11:30:50 GMT

======r ========

Sun, 20 Dec 2015 11:30:50 GMT

======s ========

2015-12-20T11:30:50

======t ========

11:30 AM
11:30 AM
11:30
11:30

======T ========

11:30:50 AM
11:30:50 AM
11:30:50
11:30:50

======u ========

2015-12-20 11:30:50Z

======U ========

Sunday, December 20, 2015 4:30:50 AM
Sunday, December 20, 2015 04:30:50 AM
Sunday, December 20, 2015 4:30:50
Sunday, December 20, 2015 04:30:50
December 20, 2015 4:30:50 AM
December 20, 2015 04:30:50 AM
December 20, 2015 4:30:50
December 20, 2015 04:30:50
Sunday, 20 December, 2015 4:30:50 AM
Sunday, 20 December, 2015 04:30:50 AM
Sunday, 20 December, 2015 4:30:50
Sunday, 20 December, 2015 04:30:50
20 December, 2015 4:30:50 AM
20 December, 2015 04:30:50 AM
20 December, 2015 4:30:50
20 December, 2015 04:30:50

======Y ========

December, 2015

======y ========

December, 2015

DateTime.Parse(string)

Wenn Sie einen formatstandardmäßigen String Date haben, können Sie einfach durch static Methode DateTime.Parse(string) zum Objekt DateTime wandeln. Normalerweise sind die Datumstring auf Internet die Formatstandardmäßigen String. Die Database MySQL oder SQL Server benutzt auch die standardmäßige Format um die Datum und die Zeit anzuzeigen
// Taken from HTTP header
string httpTime = "Fri, 21 Feb 2011 03:11:31 GMT";

// Taken from w3.org
string w3Time = "2016/05/26 14:37:11";

// Taken from nytimes.com
string nyTime = "Thursday, February 26, 2012";

// Taken from ISO Standard 8601 for Dates
string isoTime = "2016-02-10";

// Taken from Windows file system Created/Modified
string windowsTime = "11/21/2015 11:35 PM";

// Taken from Windows Date and Time panel
string windowsPanelTime = "11:07:03 PM";

7- Die Format DateTime anpassen

In diesem Part führe ich bei der Umwandlung eines Objekt DateTime zu einem String mit custom Formatierung, zum Beispiel  "dd/MM/yyyy",.... und umgekehrt

 DateTime --> string anpassen:

DateTime.ToString(string) benutzen:
string format = "dd/MM/yyyy HH:mm:ss";

DateTime now = DateTime.Now;

// ==> 18/03/2016 23:49:39
string s = now.ToString(format);
CustomDateTimeFormatExample.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class CustomDateTimeFormatExample
    {

        public static void Main(string[] args)
        {
            // A date and time format.
            string format = "dd/MM/yyyy HH:mm:ss";

            DateTime now = DateTime.Now;

            string s = now.ToString(format);

            Console.WriteLine("Now: " + s);

            Console.Read();

        }

    }

}
Custom string --> DateTime
public static DateTime Parse(string s)

public static DateTime Parse(string s, IFormatProvider provider)

public static DateTime Parse(string s, IFormatProvider provider, DateTimeStyles styles)


public static bool TryParseExact(string s, string format,
           IFormatProvider provider, DateTimeStyles style,
           out DateTime result)

public static bool TryParseExact(string s, string[] formats,
           IFormatProvider provider, DateTimeStyles style,
           out DateTime result)
Example:
Method Example
static DateTime Parse(string) // See also Standard DateTime format.
string httpHeaderTime = "Fri, 21 Feb 2011 03:11:31 GMT";
DateTime.Parse(httpHeaderTime);
static DateTime ParseExact(
  string s,
  string format,
  IFormatProvider provider
)
string dateString = "20160319 09:57";
DateTime.ParseExact(dateString ,"yyyyMMdd HH:mm",null);
static bool TryParseExact(
   string s,
   string format,
   IFormatProvider provider,
   DateTimeStyles style,
   out DateTime result
)
This method is very similar to ParseExact, but it returns a boolean, true if the time series is parsed, otherwise it returns false.
  (See example below).
ParseDateStringExample.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateTimeTutorial
{
    class ParseDateStringExample
    {

        public static void Main(string[] args)
        {

            string dateString = "20160319 09:57";

            // Use ParseExact to parse a String into DateTime.
            DateTime dateTime = DateTime.ParseExact(dateString, "yyyyMMdd HH:mm", null);

            Console.WriteLine("Input dateString: " + dateString);

            Console.WriteLine("Parse Results: " + dateTime.ToString("dd-MM-yyyy HH:mm:ss"));

            // A Datetime string with leading white space
            dateString = "  20110319 11:57";

            // Using TryParseExact method.
            // This method returns true if the 'dateString' can be parsed.
            // And return value for parameter 'out dateTime'. 
            bool successful = DateTime.TryParseExact(dateString, "yyyyMMdd HH:mm", null,
                     System.Globalization.DateTimeStyles.AllowLeadingWhite,
                     out dateTime);

            Console.WriteLine("\n------------------------\n");

            Console.WriteLine("Input dateString: " + dateString);

            Console.WriteLine("Can Parse? :" + successful);

            if (successful)
            {
                Console.WriteLine("Parse Results: " + dateTime.ToString("dd-MM-yyyy HH:mm:ss"));
            }


            Console.Read();
        }
    }

}
Das Beispiel durchführen

View more categories: