从字符串中设置日期时间选择器(日期和时间)

时间:2015-01-26 09:05:51

标签: c# datetimepicker

我有一个日期时间选择器。我想从字符串中读取数据,如下所示:

  

27/11/2014 17:35:59

设置日期时间选择器值的最佳方法是什么?

将会感谢代码示例。

5 个答案:

答案 0 :(得分:7)

我假设你在询问Winform控件" DateTimePicker"。

我们可以通过浏览DateTimePicker对象的属性来设置所需的格式。附上截图以便于参考。

enter image description here

答案 1 :(得分:4)

您可以使用DateTime.ParseDateTime.ParseExact将字符串转换为DateTime对象。您的DateTime选择器应支持此类型。

答案 2 :(得分:4)

<强> DateTime.Parse

DateTime.Parse支持多种格式。它是多才多艺的。它可以激发FormatException。

using System;

class Program
{
    static void Main()
    {
        // Taken from my head
        string simpleTime = "1/1/2000";
        DateTime time = DateTime.Parse(simpleTime);
        Console.WriteLine(time);

        // Taken from HTTP header
        string httpTime = "Fri, 27 Feb 2009 03:11:21 GMT";
        time = DateTime.Parse(httpTime);
        Console.WriteLine(time);

        // Taken from w3.org
        string w3Time = "2009/02/26 18:37:58";
        time = DateTime.Parse(w3Time);
        Console.WriteLine(time);

        // Taken from nytimes.com
        string nyTime = "Thursday, February 26, 2009";
        time = DateTime.Parse(nyTime);
        Console.WriteLine(time);

        // Taken from this site
        string perlTime = "February 26, 2009";
        time = DateTime.Parse(perlTime);
        Console.WriteLine(time);

        // Taken from ISO Standard 8601 for Dates
        string isoTime = "2002-02-10";
        time = DateTime.Parse(isoTime);
        Console.WriteLine(time);

        // Taken from Windows file system Created/Modified
        string windowsTime = "2/21/2009 10:35 PM";
        time = DateTime.Parse(windowsTime);
        Console.WriteLine(time);

        // Taken from Windows Date and Time panel
        string windowsPanelTime = "8:04:00 PM";
        time = DateTime.Parse(windowsPanelTime);
        Console.WriteLine(time);
    }
}

<强>输出

1/1/2000 12:00:00 AM
2/26/2009 7:11:21 PM
2/26/2009 6:37:58 PM
2/26/2009 12:00:00 AM
2/26/2009 12:00:00 AM
2/10/2002 12:00:00 AM
2/21/2009 10:35:00 PM
2/26/2009 8:04:00 PM

<强> DateTime.ParseExact

您必须为DateTime.ParseExact指定确切的格式字符串。您需要使用一个格式字符串,其中包含字母,告诉ParseExact在哪里读取字符串中的值。

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
    string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
    string format = "ddd dd MMM h:mm tt yyyy";

    DateTime dateTime = DateTime.ParseExact(dateString, format,
    CultureInfo.InvariantCulture);
    Console.WriteLine(dateTime);
    }
}

<强>输出

6/16/2008 8:30:00 AM

答案 3 :(得分:3)

使用它:

dateTimePicker.Value = DateTime.ParseExact("dd/MM/yyyy HH:mm:ss", 
   dateTimeString, CultureInfo.InvariantCulture);

答案 4 :(得分:3)

        string pattern = "dd/MM/yyyy HH:mm:ss";
        DateTime parsedDate;

        string dateValue = "27/11/2014 17:35:59";
        if (DateTime.TryParseExact(dateValue, pattern, null,
                                  DateTimeStyles.None, out parsedDate))
        {
            MessageBox.Show(string.Format("Converted '{0}' to {1:d}.", dateValue, parsedDate));
        }
        else
        {
            MessageBox.Show(string.Format("Unable to convert '{0}' to a date and time.",
                              dateValue));
        }