TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Huy Phan
NA
5
667
How to parse this string to Datetime C#
Sep 18 2018 9:49 PM
Hi,
I have the following code:
public
DateTime? ParseDateTime(
string
dateToParse,
string
[] formats =
null
,
IFormatProvider provider =
null
,
DateTimeStyles styles = DateTimeStyles.AssumeLocal)
{
string
[] CUSTOM_DATE_FORMATS =
{
"yyyy-MM-ddTHH:mm:ss"
,
"yyyy-MM-ddTHH:mm:ssz"
,
"yyyy-MM-dd"
};
if
(formats ==
null
)
{
formats = CUSTOM_DATE_FORMATS;
}
foreach
(var format
in
formats)
{
DateTime validDate;
if
(format.EndsWith(
"Z"
))
{
if
(DateTime.TryParseExact(dateToParse, format,
provider,
DateTimeStyles.AssumeUniversal,
out
validDate))
{
return
validDate;
}
}
else
{
if
(DateTime.TryParseExact(dateToParse, format,
provider, styles,
out
validDate))
{
return
validDate;
}
}
}
return
null
;
}
Input:
dateToParse = 2018-10-02T18:36:12
Output:
null
So how to fix it?
Thank you.
Reply
Answers (
1
)
MVC 4 Project - Bundling and Minification is not working
Differents Type of declare Pagination in Types and Methods ?