I have a need to convert a string value in the form "YYYYMMDDHHMMSS" to a DateTime. But not sure on how, may be a DateTime.Tryparse can be used to make this happen. Or is there any other way to do it. I can do this using some string operations to take "YYYYMMDD" alone, convert to a datetime and then add HH, MM, SS separately to that DateTime. But is there any DateTime.TryParse() methods that I can use in one line to convert a "YYYYMMDDHHMMSS" format string value to a DateTime value?
-
1Exact duplicate of stackoverflow.com/questions/3025361/…GalacticCowboy– GalacticCowboy2010-06-11 20:23:34 +00:00Commented Jun 11, 2010 at 20:23
-
2Actually, it's the flipside of the original question. Almost worth a -1 for doing this.Robaticus– Robaticus2010-06-11 20:24:10 +00:00Commented Jun 11, 2010 at 20:24
-
I agree. But I did not realise that its much straight forward.SARAVAN– SARAVAN2010-06-11 20:34:10 +00:00Commented Jun 11, 2010 at 20:34
-
You asked your first question a full half hour before you asked your second question. You could have modified your original question to include this instead of asking a whole new question. Frankly, the answers to your first question give you almost all the information you need to answer this question.Robaticus– Robaticus2010-06-11 20:40:00 +00:00Commented Jun 11, 2010 at 20:40
Add a comment
|
3 Answers
Define your own parse format string to use.
string formatString = "yyyyMMddHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample,formatString,null);
In case you got a datetime having milliseconds, use the following formatString
string format = "yyyyMMddHHmmssfff"
string dateTime = "20140123205803252";
DateTime.ParseExact(dateTime ,format,CultureInfo.InvariantCulture);
Thanks
2 Comments
Mark
What do you do if the date you have has the number of hours to be added or subtracted from the GMT date? An example of one of these datetimes would be "20100611221912-0500". Any advice on how to convert that would be appreciated. I don't want to make a new question cause it may be too similar to this one but at the same time, I don't know the answer to this. Thanks, Mark.
Mikael Svenson
@Mark include time zone in the string. E.g.: yyyyMMddHHmmsszzz
You have to use a custom parsing string. I also suggest to include the invariant culture to identify that this format does not relate to any culture. Plus, it will prevent a warning in some code analysis tools.
var date = DateTime.ParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
Comments
class Program
{
static void Main(string[] args)
{
int transactionDate = 20201010;
int? transactionTime = 210000;
var agreementDate = DateTime.Today;
var previousDate = agreementDate.AddDays(-1);
var agreementHour = 22;
var agreementMinute = 0;
var agreementSecond = 0;
var startDate = new DateTime(previousDate.Year, previousDate.Month, previousDate.Day, agreementHour, agreementMinute, agreementSecond);
var endDate = new DateTime(agreementDate.Year, agreementDate.Month, agreementDate.Day, agreementHour, agreementMinute, agreementSecond);
DateTime selectedDate = Convert.ToDateTime(transactionDate.ToString().Substring(6, 2) + "/" + transactionDate.ToString().Substring(4, 2) + "/" + transactionDate.ToString().Substring(0, 4) + " " + string.Format("{0:00:00:00}", transactionTime));
Console.WriteLine("Selected Date : " + selectedDate.ToString());
Console.WriteLine("Start Date : " + startDate.ToString());
Console.WriteLine("End Date : " + endDate.ToString());
if (selectedDate > startDate && selectedDate <= endDate)
Console.WriteLine("Between two dates..");
else if (selectedDate <= startDate)
Console.WriteLine("Less than or equal to the start date!");
else if (selectedDate > endDate)
Console.WriteLine("Greater than end date!");
else
Console.WriteLine("Out of date ranges!");
}
}
2 Comments
Tuncay Uyar
Selected Date : 10.10.2020 21:00:00 Start Date : 8.10.2020 22:00:00 End Date : 9.10.2020 22:00:00 Greater than end date!
Ruli
may you at least provide some basic description to the code? I am not really sure if this is answer or question what have you posted according to your comment