I am trying to convert the
string date = "31.03.2013"
to a DateTime. Here is my code:
Convert.ToDateTime(date,CultureInfo.InvariantCulture);
I am getting a Format Exception.
According to http://msdn.microsoft.com/en-us/library/vstudio/cc165448.aspx it should work.
Thanks
The invariant culture is based on en-US, where . is not a the date separator.
You need to use the correct culture, such as fr-FR, which does use . as a date separator.
You could also use DateTime.ParseExact or DateTime.TryParseExact with the exact format string.
Convert.ToDateTime("31.03.2013", CultureInfo.GetCultureInfo("fr-FR"))
Or
DateTime.ParseExact("31.03.2013",
"dd.MM.yyyy",
CultureInfo.InvariantCulture)
Will work.
How about
DateTime.ParseExact(date,"dd.MM.yyyy",null);
You could try to use the DateTime.ParseExact method. It should do the trick for you.
DateTime.ParseExact("31.03.2013", "dd.MM.yyyy", CultureInfo.InvariantCulture);
Use this code --
DateTime.ParseExact(date, "dd.MM.yyyy", CultureInfo.InvariantCulture);
You can try this with DateTime.TryParseExact :
string date = "31.03.2013";
DateTime dateConverted;
DateTime.TryParseExact(date, new string[] { "dd.MM.yyyy" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dateConverted);
Console.WriteLine(dateConverted);
Related
I have some text in rows as yyyymmdd (eg: 20181211) which I need to convert to dd/MM/yyyy
I am using:
cashRow["BusinessDate"] = Convert.ToDateTime(row.Cells[ClosingDate.Index].Value.ToString()).ToString("dd/MM/yyyy");
I get the error "string was not recognized as a valid DateTime.
Any ideas where I am going wrong?
You'll need to use ParseExact (or TryParseExact) to parse the date:
var date = "20181217";
var parsedDate = DateTime.ParseExact(date, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
var formattedDate = parsedDate.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
Here we tell ParseExact to expect our date in yyyyMMdd format, and then we tell it to format the parsed date in dd/MM/yyyy format.
Applying it to your code:
cashRow["BusinessDate"] = DateTime.ParseExact(row.Cells[ClosingDate.Index].Value.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
You have to use ParseExact to convert your yyyyMMdd string to DateTime as follows and then everything is okay.
cashRow["BusinessDate"] = DateTime.ParseExact(row.Cells[ClosingDate.Index].Value.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
As you already know the exact format of your input yyyyMMdd (eg: 20181211), just supply it to DateTime.ParseExact() and then call ToString() on the returned DateTime object.
string YourOutput = DateTime.ParseExact(p_dates, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Try this:
cashRow["BusinessDate"] = row.Cells[ClosingDate.Index].Value.ToString("dd/MM/yyyy");
I have a particular loop where DateTime instances are to be generated. My problem is on how does the class interpret the input string.
The incoming input strings are of the format MM/dd/yyyy.
Suppose I have "1/17/2014", DateTime would interpret this as MM/dd/yyyy.
But if I have "6/5/2014", how will I be sure that DateTime will parse this with the format MM/dd/yyyy and not dd/MM/yyyy?
EDIT: Inputs may come with the month and/or day in one- or two-digit format.
Because the dates could come in either MM/dd/yyyy or M/d/yyyy then the overload that takes a string[] is the most appropriate:
var dt = DateTime.ParseExact(input,
new[] { "M/d/yyyy", "MM/dd/yyyy" },
CultureInfo.InvariantCulture,
DateTimeStyles.None);
Now, regardless of the zero-padding it will work as expected.
Use the ParseExact function to specify the format :
DateTime d = DateTime.ParseExact("6/5/2014", "M/d/yyyy", CultureInfo.InvariantCulture);
If your input are in MM/dd/yyyy format, you will get 06/05/2014 instead of 6/5/2014. You will then have to use :
DateTime d = DateTime.ParseExact("06/05/2014", "MM/dd/yyyy", CultureInfo.InvariantCulture);
Be sure of your input format if you don't want to have an exception.
take a look at DateTime.ParseExact, which will allow you to specifically match the string
I have timestamp strings in the following format 5/1/2012 3:38:27 PM. How do I convert it to a DateTime object in c#
var date = DateTime.ParseExact("5/1/2012 3:38:27 PM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
You input string looks like in en-us format, which is M/d/yyyy h/mm/ss tt. You have to use proper CultureInfo instance while parsing:
var ci = System.Globalization.CultureInfo.GetCultureInfo("en-us");
var value = DateTime.Parse("5/1/2012 3:38:27 PM", ci);
or
var ci = new System.Globalization.CultureInfo("en-us");
Try to use DateTime.ParseExact method like;
string s = "5/1/2012 3:38:27 PM";
DateTime date = DateTime.ParseExact(s, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
Output will be;
01.05.2012 15:38:27
Be aware, this output can change based which Culture you used. Since my Culture is tr-TR, the date operator is . our culture.
Here is a DEMO.
Try the DateTime.ParseExact method
http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For
this maybe helps you. There you can find a detailled explanation of the ParseExact parameters.
In database datetime is being stored in MM-dd-yyyy HH:mm:ss fromat.
However, I want to display datetime in "MM/dd/yyyy HH:mm" format.
I tried it by using String.Format().
txtCampaignStartDate.Text = String.Format("{0:MM/dd/yyyy
HH:mm}",appCampaignModel.CampaignStartDateTime);
Here appCampaignModel.CampaignStartDateTime is DateTime object having value in "MM-dd-yyyy HH:mm" format.
I want to display in "MM/dd/yyyy HH:mm" format.
Can anyone help me for this?
The slashes in this format MM/dd/yyyy HH:mm mean: "replace me with the actual separator of the current culture". You have to use CultureInfo.InvariantCulture explicitely:
txtCampaignStartDate.Text = appCampaignModel.CampaignStartDateTime.ToString("MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
The "/" Custom Format Specifier
In database datetime is being stored in MM-dd-yyyy HH:mm:ss fromat.
Don't store datetimes with a format which means that you store them as (n)varchar. Use datetime instead.
txtCampaignStartDate.Text = appCampaignModel.CampaignStartDateTime
.ToString().Replace("-","/");
or
String.Format("{0:MM/dd/yyyy HH:mm}", dt);
Check : String Format for DateTime [C#]
or
String date = dt.ToString("MM/dd/yyyy HH:mm", DateTimeFormatInfo.InvariantInfo);
DateTime.ToString Method (String, IFormatProvider)
Why not just a simple replace?
txtCampaignStartDate.Text = BadString.Replace("-","/");
try using an added culture info:
CultureInfo ci = new CultureInfo("en-US");
txtCampaignStartDate.Text =
appCampaignModel.CampaignStartDateTime.ToString("MM/dd/yyyy HH:mm", ci);
p.s. the culture info used is an example.
To display your date in format you specified:
txtCampaignStartDate.Text = appCampaignModel.CampaignStartDateTime.ToString("g",DateTimeFormatInfo.InvariantInfo)
And for more date time formats you can go through this link.
Since you already have a DateTime object, just ToString it how you want:
appCampaignModel.CampaignStartDateTime.ToString("MM/dd/yyyy HH:mm");
I try to parse a date-time stamp from an external system as following:
DateTime expiration;
DateTime.TryParse("2011-04-28T14:00:00", out expiration);
Unfortunately it is not recognized. How can I parse this successfully?
sl3dg3
Specify the exact format you want in DateTime.TryParseExact:
DateTime expiration;
string text = "2011-04-28T14:00:00";
bool success = DateTime.TryParseExact(text,
"yyyy-MM-ddTHH:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out expiration);
you can use DateTime.TryParseExact instead.
How to create a .NET DateTime from ISO 8601 format
Try this
DateTime expiration;
DateTime.TryParse("2011-04-28 14:00:00", out expiration);
Without using "T".
You need to append "00000Z" to your string argument.
DateTime.TryParse("2011-04-28T14:00:0000000Z", out expiration);
User DateTime.TryParseExact function as following:
DateTime dateValue;
if (DateTime.TryParseExact(dateString, "yyyy-MM-ddTHH:mm:ss",
new CultureInfo("en-US"),
DateTimeStyles.None,
out dateValue))
{
// Do Something ..
}
Read about DateTime.