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.
Related
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");
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 am trying to get the created date and time of a particular file and then format it from 4/9/2016 to 040916. I end up with the result of 56DD16. I am not entirely sure where this value is coming from. I have used this method to format dates before without any problem. The code is below:
FileInfo fi = new FileInfo(Path.Combine(r.getCompanyFilesLocation(), r.getNyseFileName()));
DateTime dateCreated = fi.CreationTime;
string archiveFileName = dateCreated.ToString("mmDDyy");
You are using incorrect format string,mm Represent the Minute and there is nothing for DD, dd stands for day. so Change your formatString as MMddyy. to get the expected output. Here you can find more formatting options
string archiveFileName = dateCreated.ToString("MMddyy");
.ToString("mmDDyy") is incorrect. The format should be .ToString("MMddyy")
You can always review the Custom Date and Time Format Strings.
You were using wrong format. Use this one and you will get the exact same output you required:
DateTime dateCreated = fi.CreationTime;
string archiveFileName = dateCreated.ToString("MMddyy", CultureInfo.InvariantCulture);
I am trying to convert the DateTime to following Format.
2015-06-11 07:14:03.930
I have tried with ,
string plannedStartTime = startTime.ToString("o");
output:2015-06-12T16:54:47.3206929+05:30
and
string plannedStartTime = startTime.ToString("u");
output:2015-06-12 16:56:57Z
Not getting any formatters from MSDN
Any other Formatters?
all you need is a right format string
try using this startTime.ToString("yyyy-MM-dd HH:mm:ss.fff")
There is no standard date and time format for your output. You need to use custom date and time format specifiers with a culture that have : as a TimeSeparator like InvariantCulture;
string plannedStartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
If your CurrentCulture already have : as a TimeSeparator, you don't need to pass second parameter in ToString method.
Hope this is what your asking for
string plannedStartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
I have tried out as many suggestions as I've found on Stackoverflow, but not getting the desired result. Any help would be much appreciated.
My date string is "04-Dec-2013 14:14:02.143" and I want to convert this exactly as into DateTime format.
This was the last suggestion I tried:
String MyString;
MyString = "04-Dec-2013 14:14:02.143";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "dd-MMM-yyyy HH:mm:ss.fff",
null);
However, I keep getting the undesired result of "04/12/2013 14:14:02" rather want it to be "04-Dec-2013 14:14:02.143".
Any suggestions?
Yes, you should read about DateTime struct. It has not have any format info attached, it's just a plain number representing point in time.
The format come into play when you try to get string representation of the data, using ToString(format) method.
Use the format string every time you're calling ToString to get the date in the format you want it to be:
var stringDateRespresentation = dateValue.ToString("dd-MMM-yyyy HH:mm:ss.fff");
To make things easier you should pass plain, non-formatted DateTime instances all around and change it into string using ToString method only when it's being presented to the user.
#MarcinJuraszek has better answer on this question, but it does not solve the problem of different datetime patterns, because of what great SO users mark other questions as duplicate.
string MyString = "Dec-12-2013 14:14:02.143";
CultureInfo ukCulture = new CultureInfo("en-GB");
ukCulture.DateTimeFormat.ShortDatePattern = "MMM-dd-yyyy HH:mm:ss.fff";
DateTime myDateTime = DateTime.Parse(MyString, ukCulture.DateTimeFormat);
string QbDate = myDateTime.ToString("dd-MMM-yyyy HH:mm:ss.fff");
To solve the issue of different date-time patterns override any date pattern of any culture
As mentioned by #MarcinJuraszek the format comes into picture when you convert it back to string. The format you mentioned in ParseExact is for the parsing. i.e. How will MyString be parsed to create a DateTime object.
See below example just so you understand how the ParseExact format string is used.
If you use MyDateTime.ToString("dd-MMM-yyyy HH:ss:mm.fff"); instead (see I swapped mm and ss) in ParseExact you will get "12/4/2013 2:02:14 PM" instead of "12/4/2013 2:14:02 PM"