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);
Related
I have a string like this:
250920111414
I want to create a DateTime object from that string. As of now, I use substring and do it like this:
string date = 250920111414;
int year = Convert.ToInt32(date.Substring(4, 4));
int month = Convert.ToInt32(date.Substring(2, 2));
...
DateTime dt = new DateTime(year, month, day ...);
Is it possible to use string format, to do the same, without substring?
Absolutely. Guessing the format from your string, you can use ParseExact
string format = "ddMMyyyyHHmm";
DateTime dt = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
or TryParseExact:
DateTime dt;
bool success = DateTime.TryParseExact(value, format,
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
The latter call will simply return false on parse failure, instead of throwing an exception - if you may have bad data which shouldn't cause the overall task to fail (e.g. it's user input, and you just want to prompt them) then this is a better call to use.
EDIT: For more details about the format string details, see "Custom Date and Time Format Strings" in MSDN.
You could use:
DateTime dt = DateTime.ParseExact(
date,
"ddMMyyyyHHmm",
CultureInfo.InvariantCulture);
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);
DateTime Formats
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 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);
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"
I have an object "2/17/2011 6:46:01 PM".I want to convert this object to 6:46 PM
string myDateString = "2/17/2011 6:46:01 PM";
DateTime datetime = DateTime.Parse(myDateString);
string timeString = datetime.ToShortTimeString();
Console.WriteLine(timeString); // 6:46 PM
You can format the parsed datetime to a string in many other ways, but the ToShortTimeString does exactly what you want.
You can format the object as
strdate = convert.todatetime(object);
strdate .tostring("hh:mm tt");
or
strdate.toshorttime();
May be you need just format?!
DateTime.Parse(obj.ToString()).ToString("h:mm tt");
using System.Globalization;
...
string dateString, format;
format = "M/dd/yyyy h:mm:ss tt";
dateString = "2/17/2011 6:46:01 PM";
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine(result.ToString());
See here for more info:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
If input is a string in first convert it to a dateTime by DateTime.parse method and then convet it to shortTimeString Or other
If input is DateTime convert it to shorttimeString in this form : input.toShortTimeString