I am getting some dates in string in an ajax post to the controller.
Code
public ActionResult ModifyPartnerForOrganization(Guid partnerID, string aliasName, string effectiveDate, string expirationDate)
{
Partner partner = new Partner();
partner.PartnerId = partnerID;
partner.ExternalId = aliasName;
partner.EffectiveDate = DateTime.ParseExact(effectiveDate, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
partner.ExpirationDate = DateTime.ParseExact(expirationDate, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
}
The date coming from client side are in MM-DD-YYYY format for example 01/10/2014(just for explanning adding the 01/oct/2014) to controller as10/1/2014.
When trying to parse get this error.String was not recognized as a valid DateTime.
Any help on this??.
So okay, the format received from the client is mm/dd/yyyy. Then you should parse it like that, not like yyyyMMdd:
DateTime.ParseExact(effectiveDate, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Also note the use of MM instead of mm, since mm is minutes, not months.
All the formats you specified so far:
string effectiveDate = "01/10/2014";
string effectiveDate2 = "30/oct/2014";
string effectiveDate3 = "1/10/2014";
DateTime d = DateTime.ParseExact(effectiveDate, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
DateTime d2 = DateTime.ParseExact(effectiveDate2, "dd/MMM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
DateTime d3 = DateTime.ParseExact(effectiveDate3, "d/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Related
I have a datetime string in the format "MM-dd-YY hh:mmtt". For example: 02-23-21 09:23PM. I am trying to convert this to a DateTime object in C#. I'm using DateTime.TryParseExact() but it keeps failing.
Here is how I'm converting it:
var myDateTime = new DateTime();
var success = DateTime.TryParseExact(
"02-23-21 09:23PM",
"MM-dd-YY hh:mmtt",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out myDateTime
);
TryParseExact() keeps returning false and myDateTime keeps coming out {0001-01-01 12:00:00 AM}.
Can anyone see what I'm doing wrong?
You have an error in your year format - yy is correct one:
var success = DateTime.TryParseExact(
"02-23-21 09:23PM",
"MM-dd-yy hh:mmtt",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out myDateTime
);
From the docs:
The "yy" custom format specifier represents the year as a two-digit number
I read date-time strings from a file in 2 different formats:
19/02/2019 08:24:59
2/17/2019 12:25:46 PM
For the first format the custom format string I wrote is:
string firstDate = "19/02/2019 08:24:59";
string customFormatForFirstDateTimeString = "dd/mm/yyyy hh:mm:ss";
and I use it as follows:
string firstResultingDateAndTime;
bool parsingSuccessful = DateTime.TryParseExact(
firstDate,
customFormatForFirstDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out firstResultingDateAndTime);
The problem is that parsingSuccessful results false.
For the second date-time string, the code is as follows:
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "m/dd/yyy hh:mm:ss PM";
string secondResultingDateAndTime;
parsingSuccessful = DateTime.TryParseExact(
secondDate,
customFormatForSecondDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out secondResultingDateAndTime);
Also here I receive
parsingSuccessful == false;
I reckon that the custom format strings do not fit the date-time strings, but I was not able to figure out why.
Please help.
Thank you in advance.
Well, mm stands for minutes, not months (we have MM for it) that's why dd/mm/yyyy format should be dd/MM/yyyy.
Another issue with hour format where we have hh for 0..12 range (with tt for AM/PM) and HH for 0..23 interval:
string firstDate = "19/02/2019 08:24:59";
// Since we don't have AM / PM we can conclude that hour is in 0..23 range
string customFormatForFirstDateTimeString = "dd/MM/yyyy HH:mm:ss";
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "M/dd/yyyy hh:mm:ss tt";
I have string in format MMddyy for example '112192' which I need to convert in DateTime object.
If I tried to split string and use new DateTime(yy, mm, dd); but is sets year as 0092. But I needed it as 1992.
I am also tried this:
DateTime.TryParseExact(str, "MMddyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date);
But it returns date with DateTime.MinValue
Alternatively, you can do it by this way
string str = "112192";
DateTime date = DateTime.ParseExact(str, "MMddyy", CultureInfo.InvariantCulture);
Console.WriteLine(date.ToString("yyyy"));
Also, your approach is working
DateTime date;
string str = "112192";
bool success = DateTime.TryParseExact(str, "MMddyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date);
Console.WriteLine(date.ToString("yyyy"));
I solved the same issue like this:
string strdate = "9/10/2017"; // mm/dd/yyyy
DateTime oDate = DateTime.ParseExact(strdate, "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture); Console.WriteLine(oDate.Day);
Console.WriteLine(oDate.Day);
I am getting this date time string from Sharepoint:
2013-01-01 08:00:00:000
I want to convert it to
2013-01-01 08:00:00 AM
Any ideas how to do that? Every time I try to use Date.Parse, I get an error saying the string is not a valid date time type
Best option is parsing it to DateTime and get it's string representation with a specific format.
string s = "2013-01-01 08:00:00:000";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture).Dump();
// 2013-01-01 08:00:00 AM
}
Based on Jon's comment, you can use ParseExact as well.
DateTime dt = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture);
string s = dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);
Or you can format directly that ParseExact return value;
string s = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);
I would like to have my end result in date format as per the specified format i.e YYMMDD how can i get this from a string given as below
string s="110326";
From string to date:
DateTime d = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
Or the other way around:
string s = d.ToString("yyMMdd");
Also see this article: Custom Date and Time Format Strings
DateTime dateTime = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
although id recommend
DateTime dateTime;
if (DateTime.TryParseExact(s, "yyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime))
{
// Process
}
else
{
// Handle Invalid Date
}
To convert DateTime to some format, you could do,
string str = DateTime.Now.ToString("yyMMdd");
To convert string Date in some format to DateTime object, you could do
DateTime dt = DateTime.ParseExact(str, "yyMMdd", null); //Let str="110719"