C# - How to convert string date to DateTime format - c#

I have a string date
"7.08.2014"
and want to convert into this format :
2014-07-08
I tried a no of solution that previously propoed on stackoverflow, unfortunately none of work for me. Any suggestion please, here is my code
var parsedDate = DateTime.ParseExact(match.date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
It keep throws an error
Additional information: String was not recognized as a valid DateTime.

You're using the wrong format. Try this:
var parsedDate = DateTime.ParseExact(match.date, "d.MM.yyyy", CultureInfo.InvariantCulture);
DateTime.ParseExact attempts to convert a string representation of a datetime to a DateTime by using the format provided in the second parameter.
To obtain a string in the other format you have to call ToString on parsedDate with a custom format specifier:
var dateInCustomFormat = parsedDate.ToString("yyyy-dd-MM", CultureInfo.InvariantCulture);

Related

c# convert yyyymmdd text to dd/MM/yyyy

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");

ParseExact Not working

I am trying to convert my string to datetime using ParseExact method but it is not working as expected, Date format in the string is "dd/MM/yyyy" but when i use parseExact method, it changes the format to "MM/dd/yyyy". i want to keep my date format as it is in the string and just want to change string to DateTime. here is my code given below.
string FormattedDate = "18/03/2017";
var parsed = DateTime.ParseExact(FormattedDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
It returns "03/18/2017", how i can keep it same.
please help.
Thanks
It is working, since the input string is parsed as DateTime object. You can't change the format of the DateTime object but you can get the value into any format by using format strings.
string oldFormat = parsed.ToString("dd/MM/yyyy");
string anotherFormat = parsed.ToString("yyyy-MMMM-dd");

Converting this string format to a valid DateTime?

The string in question: "2003:12:14 12:01:44" (yyyy:MM:dd hh:mm:ss)
How would I be able to convert this to a valid datetime in C#? I tried Convert.ToDateTime(str), but to no avail.
Use the right DateTime format and supply DateTime.ParseExact with that format. Note that since your time doesn't show AM or PM, it may be safer to assume that it uses 24-Hr format (use capital HH) than 12-Hr AM PM (not hh) format. The following code should work:
string format = "yyyy:MM:dd HH:mm:ss"; //note: use HH not hh
var result = DateTime.ParseExact("2003:12:14 12:01:44", format, CultureInfo.InvariantCulture);
Check more on the available DateTime formats here (standard) and here (custom).
Convert.ToDateTime try to parse your string as a standard date and time format of your CurrentCulture settings. Looks like this string is not a standard format.
You can use DateTime.ParseExact for specify custom format.
var dt = DateTime.ParseExact("2003:12:14 12:01:44",
"yyyy:MM:dd hh:mm:ss",
CultureInfo.InvariantCulture);

How to change custom string format to Datetime format in Asp.net mvc

I have textbox asssociated with model class having value like
"03/28/2014".
I want to convert this to DateTime with 28/03/2014 format. I have tried following ways, but I'm not getting anywhere close...
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-US", true);
string date = timesheetModel.START_DATE;
string pattern = "dd-mm-yyyy";
DateTime dt=DateTime.ParseExact(date,pattern,theCultureInfo).
DateTime dt= Convert.ToDateTime(timesheetModel.START_DATE);
and also DateTime.Parse(date);
By this getting compile time error:
"String was not recognized as a valid DateTime."
I have tried also in modal class with Types like:
public string date{get;set;} // by this type not converting to datetime
public DateTime date{get;set;} // not able to access values to controller
Is there any other solution for this? Because the textbox value format is fixed.
I guess you should Use DateTime.ParseExact with the format "dd/MM/yyyy"
var result = DateTime.ParseExact("03/28/2014", "MM/dd/yyyy", CultureInfo.InvariantCulture)
.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
you can go though the below link for more information
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

DateTime Conversion and Parsing DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")

I store some DateTime in a CSV log with:
DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
When I try to read it I found something like:
"05/15/2012 10:09:28.650"
The problem is when I try to cast it as a DateTime again...
DateTime.Parse("05/15/2012 10:09:28.650");
Throws an Exception "Invalid DateTime" or something like that...
How can I properly re-read the DateTime?
You can use DateTime.ParseExact:
string format = "MM/dd/yyyy hh:mm:ss.fff";
DateTime d = DateTime.ParseExact("05/15/2012 10:09:28.650",
format,
System.Globalization.CultureInfo.InvariantCulture);
Standard Date and Time Format Strings
use DateTime.ParseExact with specifying the format
String dateStr=DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff");
DateTime date = DateTime.ParseExact(dateStr,"MM/dd/yyyy hh:mm:ss.fff",System.Globalization.CultureInfo.InvariantCulture);
You should use this method to parse your string. You would have to make a class, imlementing IFormatProvider, but if you want to use a custom DateTime format, it's the best method I can think of.

Categories

Resources