Reputation: 77
I try to convert string from DateTimePicker
control to int but when I run it happen error
String was not recognized as a valid DateTime.
I am using epoch to convert. The format of DateTimePicker
is D/M/YYYY
. This is my code
public static double convertToEpoce(string date)
{
DateTime _datetime= DateTime.ParseExact(date, "D/M/YYYY HH:mm:ss", CultureInfo.CreateSpecificCulture("vi-VN")); ;
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToDouble((_datetime - epoch).TotalSeconds);
}
Upvotes: 1
Views: 1610
Reputation: 7517
There's no need to parse the value of DateTimePicker.Text
.
You can use DateTimePicker.Value
, which will return a DateTime-value.
The error you get, is because of the wrong format specifier, like already stated in @GeorgPatscheider's answer.
Upvotes: 1
Reputation: 9463
The format specifier you are using is not valid. It should be d/M/yyyy HH:mm:ss
in the C# code. (DateTimePicker might use different specifiers.)
"d" The day of the month, from 1 through 31.
"M" The month, from 1 through 12.
"yyyy" The year as a four-digit number.
There are no "D" or "YYYY" format specifiers.
For full list see Custom Date and Time Format Strings.
For working example see this C# Fiddle.
Upvotes: 1