Set date time picker from a string (date and time)
Solution 1
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));
}
Solution 2
I Assume you are asking about Winform control "DateTimePicker".
We can set desired format by browsing Properties for DateTimePicker Object. Screenshot attached for easy reference.
Solution 3
DateTime.Parse
DateTime.Parse supports many formats. It is versatile. It can provoke a 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);
}
}
Output
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
You must specify the exact formatting string for DateTime.ParseExact. You need to use a format string that has letters in it that tell ParseExact where to read in the values from your string.
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);
}
}
Output
6/16/2008 8:30:00 AM
Solution 4
You can use DateTime.Parse
or DateTime.ParseExact
to turn a string into a DateTime object. Your DateTime picker should support this type.
Solution 5
use this:
dateTimePicker.Value = DateTime.ParseExact("dd/MM/yyyy HH:mm:ss",
dateTimeString, CultureInfo.InvariantCulture);
Barak Rosenfeld
Updated on January 26, 2020Comments
-
Barak Rosenfeld about 4 years
I have a datetime picker. I want to read the data from a string which looks like this:
27/11/2014 17:35:59
what is the best way to set the value of the datetime picker?
A code example will be appreciated.