Csharp/CSharp Tutorial/Date Time/DateTime Parse ParseExact

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

DateTime.Parse: M

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt5 = DateTime.Parse("2:15 PM");
        Console.WriteLine(dt5);
    }
}
25/03/2007 2:15:00 PM

==DateTime.Parse: /td>


using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt3 = DateTime.Parse("5,9,05");
        Console.WriteLine(dt3);
    }
}
05/09/2005 12:00:00 AM

==DateTime.Parse: /td>


using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt4 = DateTime.Parse("5/9/2005 14:15:33");
        Console.WriteLine(dt4);
    }
}
05/09/2005 2:15:33 PM

DateTime.ParseExact: (PM, h:mm:ss tt, null)

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt6 = DateTime.ParseExact("2:13:30 PM", "h:mm:ss tt", null);
        Console.WriteLine(dt6);
    }
}
25/03/2007 2:13:30 PM

DateTime.ParseExact: Mon, 05 SepMT, ddd, dd MMM yyyy HH":"mm":"ss "GMT", null

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt7 = DateTime.ParseExact("Mon, 05 Sep 2005 14:13:30 GMT", "ddd, dd MMM yyyy HH":"mm":"ss "GMT"", null);
        Console.WriteLine(dt7);
    }
}
05/09/2005 2:13:30 PM

DateTime.ParseExact: September 05, MMMM dd, null

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt8 = DateTime.ParseExact("September 05", "MMMM dd", null);
        Console.WriteLine(dt8);
    }
}
05/09/2007 12:00:00 AM

DateTime.Parse: Monday 5 September16:31

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt2 = DateTime.Parse("Monday 5 September 2005 14:16:31");
        Console.WriteLine(dt2);
    }
}
05/09/2005 2:16:31 PM

DateTime.Parse: Sep 2005

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        // 1st September 2005 00:00:00
        DateTime dt1 = DateTime.Parse("Sep 2005");
        Console.WriteLine(dt1);
    }
}
01/09/2005 12:00:00 AM

Use the Parse() method to convert strings to DateTime instances: 4

using System;
class MainClass
{
  public static void Main()
  {
    
    DateTime myDateTime5 = DateTime.Parse("1/11/2004");
    Console.WriteLine("myDateTime5 = " + myDateTime5);
  }
}
myDateTime5 = 01/11/2004 12:00:00 AM

==Use the Parse() method to convert strings to DateTime instances: /td>


using System;
class MainClass
{
  public static void Main()
  {
    
    DateTime myDateTime5 = DateTime.Parse("1/11/2004 23:10:30");
    Console.WriteLine("myDateTime5 = " + myDateTime5);
  }
}
myDateTime5 = 01/11/2004 11:10:30 PM