Date format "dd/MM/yyyy" to "yyyy/MM/dd" - c#

I know this question has been asked before and resolved using various methods.
I am converting a value from a string to a DateTime.
Throughout the project I have used the same CultureInfo, all strings that where converted to date where done using Convert.ToDateTime(), but now there is one text field that refuses to convert.
I have tried:
string date = "27/02/2013";
string startdated = (Convert.ToDateTime(date)).ToString("yyyy/MM/dd");
(converts to datetime and changes it back to sting in my required format. This works fine on everything else)
even
Datetime dt = Convert.toDateTime(date); doesn't work
DateTime.ParseExact(date, "yyyy/MM/dd", format); doesn't work
And all give me the same error "String was not recognized as a valid DateTime.". i receive my date value from a textbox with an ajax calender extender (CalendarExtender.Format = "dd/MM/yyyy" done for display purposes,this also works everywhere else i.e "dd/MM/yyyy" for display and "yyyy/MM/dd" for procedure) except this final value which simply will not change. Everthing is done via my machine with no external servers

Your input string is not in the same format as the format you provde DateTime.ParseExact with.
For this to work
DateTime.ParseExact(date, "yyyy/MM/dd", format);
You have to enter the date in year/month/day format. But your string is in day/month/year.
This should work better.
string date = "27/02/2013";
DateTime parsedDate = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Your date is date = "27/02/2013"; and your current format (in DateTime.ParseExact) is "yyyy/MM/dd", It should be:
"dd/MM/yyyy"
So the following code should work.
string date = "27/02/2013";
DateTime dt = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You can also use the format "d/M/yyyy" which would take care of single or double digit date/month.

Related

datetime format conversion from mm-dd-yyyy to yyyy-mm-dd in c#

I am using asp net core with entity framework. I have a datetimepicker which i am using as calendar. I have customize its format using jquery like this.
$('.datepicker').datetimepicker({
format: 'yyyy-mm-dd'
});
This shows the date forma to user.
and then converting it to C# datetime I am saving it in database in same format.Every thing is correct till now.
Now on my next visit this page has to be pre-filled if data is there. This time on the textbox of calendar it is showing some thing like 0007/02/19 where as the date that i entered is 1993/07/02 and the date that is saved in database is 7/2/1993. I tried using
String DateString = Convert.ToString(ProfileDetails.DateOfBirth);
IFormatProvider culture = new CultureInfo("en-US");
DateTime dateVal = DateTime.ParseExact(DateString, "MM/dd/YYYY", CultureInfo.InvariantCulture);
DateTime date = Convert.ToDateTime(dateVal);
user.DateOfBirth = dateVal;
this but was getting error string is not in valid datetime format. In my modal I have defined date as datetime. I am not getting the solution. Can any one gives me the correct way to complete that.
Cuz you are using MM/dd/YYYY instead MM/dd/yyyy
Working code:
String DateString = Convert.ToString("07/02/1993");
IFormatProvider culture = new CultureInfo("en-US");
DateTime dateVal = DateTime.ParseExact(DateString, "MM/dd/yyyy", CultureInfo.InvariantCulture);

How to convert textbox value ddmmyyyy to dd-mm-yyyy?

I have a textbox in my project where user enters date, like ddmmyyyy. I need to convert it to dd-mm-yyyy format so that I could fetch particular data from database.
You can parse your string to DateTime first and then generate it's string representation with that format.
For example;
var s = "11062016";
var dt = DateTime.ParseExact(s, "ddMMyyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture));
By the way, I assume you wanna say MM instead of mm since mm specifier is for minutes but MM specifier is for months.
On the other hand, you never told the data you wanna fetch but, it is not a good idea to get DateTime values (if they are) with strings. Use DateTime values to get DateTime data from your database (which most of of RDMS supports), not strings.
Try
string str = "06112016";
DateTime date = new DateTime();
DateTime.TryParseExact(str, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date);
string FormattedDate = date.ToString("MM-dd-yyyy");

Unable to create date with current time from string

I am taking selected date from telerik date picker and want to take that selected date and current system time by 24 hours format and want date like this:
For Ex: Current Date = dd/mm/yy HH:Minutes:Seconds
21/1/2016 14:48:21
This is my code:
DateTime dt = DateTime.ParseExact(Datepicker1.SelectedDate.Value.ToShortDateString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);//Error:String was not recognized as a valid DateTime.
Datepicker1.SelectedDate.Value= {1/21/2016 12:00:00 AM}
Datepicker1.SelectedDate.Value.ToShortDateString()=1/21/2016
Error:String was not recognized as a valid DateTime on Datetime.ParseExact.
Change your format in the ParseExact from
"dd/MM/yyyy"
to
"M/d/yyyy" or "M/d/yyyy h:m:s tt" //the first one use .ToShortDateString(), the second one for 1/21/2016 12:00:00 AM
The first one only takes care for case like 21/12/1997
The second one takes care 12/21/1997, 2/21/1997, 12/2/1997, and 2/1/1997 in addition to take care of time info
Also, note that you may consider to have multiple formats (just in case): "d/M/yyyy H:m:s", "d/M/yyyy h:m:s tt" to take care of the case where day and month are swapped and when you have AM/PM.
MSDN link.
#yourDateTime.FormattedReviewDate.ToString("MMM dd,yyyy")
You might even just add a simple property to dateTime for the formatted display:
public string FormattedReviewDate
{
get { return ReviewDate.ToString("MMM dd,yyyy"); }
}
Note: Give your needed format
You don't need to parse anything.
Your Datepicker1.SelectedDate.Value is already DateTime, just assign this value to your dt variable.
DateTime dt = Datepicker1.SelectedDate.Value;
If you want to get it's string representation based on your format, just use ToString method with proper format.
string formattedDate = dt.ToString("dd/M/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
which returns 21/1/2016 14:48:21 as a string.
Also if you want 1/21/2016 as a string, just change your format to M/dd/yyyy like;
string formattedDate = dt.ToString("M/dd/yyyy", CultureInfo.InvariantCulture);

DateTime.TryParseExact format MM/dd/yyyy

I'm trying to convert datetime format to MM/dd/yyyy but I get result as "05/14/14" which I'm expecting to be "05/14/2014".
What's wrong in this code?
string input = Datetime.Now.ToString("MM/dd/yyyy");
DateTime d;
if (DateTime.TryParseExact(input, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d))
{
// use d
}
Update
I had short date setting in my pc to "MM/dd/yy" when I changed to "MM/dd/yyyy", it worked.
What could be the solution so that datetime should show regardless of pc setting.
string input = Datetime.Now.ToString("MM/dd/yyyy");
should be DateTime.
With this change the code runs fine and gives desired output.No need to change the date format in PC.
Also why to do all these stuff while this can be achieved with a single line of code:
var shortDate = DateTime.Now.ToString("d");
This will give the following output as "MM/dd/yyyy" irrespective of PC date format that you have mentioned.

How to Convert a Date String with Format "dd/MM/yyyy" to OS Current Culture Date Format

A string has the value in "dd/MM/yyyy" format like "04/10/2012". This should be converted to a Date w.r.t Current Culture of OS.
I have tried below string with Korean as Current Culture of OS in which date format is yyyy-MM-dd, my code is not getting correct Month value, it interchange the month value with day:
Input: "04/10/2012"
Output: 2012-04-10
Code:
DateTime DT;
string dt = "04/10/2012";
DateTimeFormatInfo DateInfo = CultureInfo.CurrentCulture.DateTimeFormat;
DT = Convert.ToDateTime(String.Format ("{0:"+DateInfo .ShortDatePattern +"}", dt.Trim ()), CultureInfo .CurrentCulture);
MessageBox.Show("Date: " + DT.ToShortDateString());
How I can fix that ?
It looks to me like you need to parse it with a fixed format, I think you are currently parsing it with a format other than "dd/MM/yyyy" and because the date is ambiguous (as in, month and day can be interchanged without causing invalid dates) the format is simply switching the month and day value. When you then go to output it, it looks reversed.
Use DateTime.ParseExact to force the parsing format and then use the built-in current culture sensitive string output methods on DateTime to get a formatted string:
var date = DateTime.ParseExact("04/10/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture);
MessageBox.Show("Date: " + date.ToShortDateString()); // Assumes current culture is controlling format
Since your input string is in a fixed format, you should parse it in that format:
DateTime.ParseExact(dt, "dd/MM/yyyy", CultureInfo.InvariantCulture);
If you string has the format dd/MM/yyyy then you have to use DateTime.ParseExact with the specified format:
DateTime.ParseExact(dt, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Anything else will try an interpret the string according to the current culture's rules - which, as you have found, will fail.
why not use ToShortDateTimeString()

Categories

Resources