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");
Related
string strDate = 16-08-1979 i.e dd-MM-yyyy
Datetime Dob=DateTime.Parse(strDate.ToString() , new CultureInfo("en-US", false));
I want every format convert into "MM/dd/yyyy"
Given the example you provided, there's a few steps you'd need to take first.
Your strDate value isn't a data type, so make it a string.
string strDate = "16-8-1979";
This is already a string that's in a bad format, but we can parse it to a DateTime value to work with.
DateTime.TryParse(strDate, out DateTime myDate);
Now we've got a DateTime to work with, we can output a properly formatted string with the value.
string output = myDate.ToString("MM/dd/yyyy"); // output should be 08/16/1979
I am trying to format a date. I get the date from the database in the dateTime format and i want to show only the date part not the time.
string date = dt.Rows[0]["declaration_date"].ToString();
string dateFormat = date.ToString("MM/DD/YYYY");
This is not working. Any help?
For date.ToString with formatting to work, date needs to be a DateTime. Try changing your code to something like this:
DateTime date = (DateTime)dt.Rows[0]["declaration_date"];
string dateFormat = date.ToString("MM/dd/yyyy");
Also, your formatting string should be MM/dd/yyyy.
You are doing it almost correctly.
Try this:
string date = dt.Rows[0]["declaration_date"].ToString("MM/dd/yyyy");
The point is that you have to apply .ToString("MM/dd/yy") to a DateTime object, not to a string.
The below code is in MM/DD/YYYY format
string dateStr="9/7/1986";
But i want to change it like below format
dateStr="09/07/1986";
again same in MM/DD/YYYY format
This code should work for you.
string dateStr = "9/7/1986";
string newDateStr= DateTime.Parse(dateStr).ToString("MM/dd/yyyy");
newDateStr will hold the value you need.
The best thing to do would be to use that format when you first convert the DateTime value to a string. Although, this would only work if you had it as a DateTime variable first.
You could parse it to a DateTime then format it back to a string.
dateStr = DateTime.ParseExact(dateStr, "M/d/yyyy", CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy");
Note that you'll get exceptions if the string doesn't match the M/d/yyyy format.
I need to write date in MM/DD/YYYY format. From a Date Picker Control. I try to assign it to a DateTime variable. Before Writing it to a file I assign it to a string. I see the value stored in String variable is in DD/MM/YYYY format.
Below is assignment statement
DateTime startTime, endTime;
string startTimeDate = "";
startTime = Convert.ToDateTime(dpStartTime.Value.ToString("MM/dd/yyyy HH:mm"));
endTime = Convert.ToDateTime(dpEndTime.Value.ToString("MM/dd/yyyy HH:mm"));
startTimeDate = startTime.ToString("MM/dd/yyyy HH:mm");
startTimeDate = startTimeDate.Replace('-', '/');
I observe startTimeDate is stored as DD/MM/YYYY only. startTime is storing as MM/DD/YYYY format only. Please let me know if there is any other approach to correctly assign / convert the date values.
Thanks in Advance
Edit:
You are losing the original date in your Convert.DateTime() conversion, you have to apply the format string here as well e.g. using DateTime.ParseExact :
startTime = DateTime.ParseExact(dpStartTime.Value.ToString("MM/dd/yyyy HH:mm"),
"MM/dd/yyyy HH:mm",
CultureInfo.InvariantCulture);
Instead of using Convert.ToDateTime(), try to use DateTime.ParseExact(string s, string format, IFormatProvider provider) when you can ensure the datetime format and want to ignore the system settings. MSDN Reference for the DateTime.ParseExact() method can be found here
You're converting DateTime to string, then you're converting it again to DateTime using Convert class. You're not preserving the format here cause DateTime is format agnostic. And default ToString conversion (used by debugger) is in your cause MM/DD/YYYY.
I usually use String.Format, ex
String.Format("{0:d/M/yyyy HH:mm:ss}", dpStartTime.Value);
To format dates as strings.
I have the following function
DateTime fromDateParam = DateTime.ParseExact(Convert.ToString(DateTime.MinValue),"dd.MM.yyyy HH:mm:ss",null);
It says input string not recognised as a valid date.
Any ideas how I can get any the min date recognised to parse exact?
Well you're converting the original time to a string using the default formatting, but then you're specifying custom formatting for the parsing.
If you specify a format string using DateTime.ToString(format) and keep the format consistent, it works fine:
string formatString = "dd.MM.yyyy HH:mm:ss";
string text = DateTime.MinValue.ToString(formatString);
Console.WriteLine(text);
DateTime fromDateParam = DateTime.ParseExact(text, formatString, null);
In other words (continuing Skeet's answer), Convert.ToString(DateTime.MinValue) is based on current/default CultureInfo, etc.