Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I know this has been asked a lot of times, but none seems to relate to my problem (all answers specifies another datetime format than the one which is giving me issues):
Convert string(dd/MM/yyyy hh:mm) to datetime format
Converting a String to DateTime
Convert dd/MM/yyyy hh:mm:ss.fff from String to DateTime in C#
Specific example:
Input is a string: 24/10/2016 10:20
I call DateTime.TryParse(input, out output)
The output is a DateTime: {1/1/0001 12:00:00 AM}
Why? This is a perfectly valid input format from what I know...
Things I tried / restrictions:
Change the input to have a second: 24/10/2016 10:20:00, it works
Use TryParseExact, specifying this format, it works
However, I cannot use both these solutions as the input is user defined, I cannot force the user to stick to a specific input, and want to accept any reasonably formatted date times. The format I specified in the question seems reasonable (it's the default format outputted by Excel).
I can assume the culture is en-US
Any help would be appreciated.
Update:
The top answer to the first question throws an exception... I don't know why that's even up-voted.
Update 2:
Since there are a lot of close requests, here's some minimal working code (duplicated from the answer by Mohit Shrivastava):
string dtstr = "24/10/2016 10:20";
DateTime outdt;
DateTime.TryParse(dtstr, out outdt);
Console.WriteLine(outdt);
Console.ReadLine();
Your output is 1/1/0001 12:00:00 AM which is the min value of the DateeTime object, which means that the conversion failed(as per this documentation). The string input is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture. So the problem is the format of the date-string that you are passing.
Try using TryParseExact method in a smarter way since you know the format of the string input, like the following:
string dateString ="24/10/2016 10:20";
string formatString="dd/MM/yyyy HH:mm";
DateTime dateValue;
CultureInfo enUS = new CultureInfo("en-US"); // is up to you
if (DateTime.TryParseExact(dateString, formatString , enUS,
DateTimeStyles.None, out dateValue))
The same code gives the correct output on my machine.
string dtstr = "24/10/2016 10:20";
DateTime outdt;
DateTime.TryParse(dtstr, out outdt);
Console.WriteLine(outdt);
Console.ReadLine();
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a date format as shown below 2021-02-11T13:00:00+04:00 which i need to get in following format 11-FEB-20 01.00.00.000000000 PM.How can i achieve it ?
Parse and then format according to Custom date and time format strings
DateTime dt = DateTime.Parse("2021-02-11T13:00:00+04:00");
Console.WriteLine(dt.ToString("dd-MMM-yy"));
You can Parse form existing format into DateTime and then format it into String with the desired format string:
string source = "2021-02-11T13:00:00+04:00";
string result = DateTime
.Parse(source)
.ToString("dd-MMM-yy hh.mm.ss.fffffff'00' tt", CultureInfo.InvariantCulture)
.ToUpper();
Please, note
ToUpper() since we want FEB, not Feb.
fffffff'00' - we can't provide 9, but 7 digits after the decimal point, so we have to append two zeros.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have date on format 11/19/2017 12:00:40 AM and I want to transform it to 2017/11/19 12:00:40 AM
var d = DateTime.ParseExact("11/19/2017 12:00:40 AM", "M/d/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture).ToString("yyyy/M/d hh:mm:ss tt");
var dt = DateTime.ParseExact(d, "yyyy/M/d hh:mm:ss tt", CultureInfo.InvariantCulture);
value of d is actually 2017/11/19 12:00:40 AM but it's string. In db I must write it as yyyy/M/d hh:mm:ss tt.
But result of dt is anyway the old value.
What's wrong?
You are running into representation of a value vs the actuall value.
In reality all dateTimes are a unsigned Int64 counting the number of Ticks since "point X" (wich is usually the start of the Unix Time, 1st Janurary 1970, 00:00:00). But users can not work with that. They need a human readable string format. Wich is also different between Langauges, much less Cultural regions. UK and US both speak english, but can not decide if the month or day should come first.
Both the ToString() and Parse() functions are designed to automagically extract the proper way a Number should look as string from the Region settings of Windows. The ToString() function that is part of the Debugger is no exception.
DateTime is backed by a number (ticks). It has no human-readable representation, at all! When you create a DateTime (by specifying each date component individually, or by parsing a date string), it converts the string date into a ticks.
So what are you seeing in the debugger? Well, that's the result of the DateTime object's default ToString() method. This depends on the culture of the thread which the code is running in. Unless you change it, it will be the same as the operating system culture by default.
Likewise, this culture is also used when parsing dates, so in the UK 5/1/2018 would be understood by DateTime.Parse(...) as the 5th of January, 2018, whereas in the US it would be understood as the 1st of May, 2018.
How should you use this information?
Understand that the date format used in the debugger is based on your computer's culture, and it means nothing as to how the DateTime object will perform on computers in other cultures.
If you want to display the date to a user, you need to use the ToString() methods (there are a few overloads) to produce a date formatted the way you want it to be. Of course, the parameterless ToString() method will work fine if you just want it formatted for the end-user's culture.
If you need to send a specific format, again you should use one of the ToString() overloads. For example, you can pass the format o: DateTime.UtcNow.ToString("o"), and it will print the date in an ISO8601 compliant format: 2018-04-28T15:31:00.000Z
You can find different format strings here (MS docs).
One last thing: "In db I must write it as yyyy/M/d hh:mm:ss tt."
If you are using an SQL database (SQL Server, MySQL, etc.) and you are using a date-typed field in the database, you should build your SQL queries using parameters. You wouldn't need to format the date at all then, you could just pass the DateTime object as a parameter.
See here for more information on the SQL injection vulnerability and how to use parameterized queries.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to convert Time string to DateTime but can't.
Example: Time: 12:05:45.458 with milliseconds need to convert to time.
Any solution?
DateTime.ParseExact should solve this
DateTime result = DateTime.ParseExact("12:05:45.458", "HH:mm:ss.fff", CultureInfo.InvariantCulture);
The DateTime.ParseExtract will parse the given time and convert to DateTime object with date as current date.
var dateTime = DateTime.ParseExact("12:05:45.458", "HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dateTime);
You are looking for a Timespan.Parse() here. Since you do not have any date component, it is not possible to unambiguously convert to a DateTime instance.
Simple solution:
string time = "12:05:45.458";
DateTime dateTime = DateTime.ParseExact(time, "HH:mm:ss.fff",
CultureInfo.InvariantCulture);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a string like below
1/1/1970 12:00:00 AM
I want it in the yyyy-MM-dd format (in this case 1970-01-01)
My parsing code is
var actualDate = DateTime.ParseExact(actualValue,"MM/dd/yyyy HH:mm:ss tt",CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
But I keep getting the error, that the string is not recognized as a valid date time.
I looked at my variable actualValue and it is of type DateTime, so am thinking that the problem is with the format MM/dd/yyyy HH:mm:ss tt ,What is wrong with this?
First, you shouldn't be storing or fetching dates as text in the DB.
To your specific issue, however, MM and dd are the two-digit variety of month and day. Obviously your date text doesn't use the two-digit-only variety, so use M and d. Also, HH is on a 24-hour clock. Using tt, which is AM/PM, would imply not having a 24 hour clock, so you would want to use hh instead.
For more, look at MSDN for custom date/time formatting.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got a DateTime in a string format :
"05/30/2014 12:00:00"
I want to create a DateTime object from this string by doing this :
DateTime startDate = DateTime.Parse(startDate);
However, I've got an error which says that this method has some invalid argument :
Error 1 - The best overloaded method match for 'System.DateTime.Parse(string)' has some invalid arguments
When I'm reading the doc, a string argument is definitely a good argument. What's wrong with what I'm doing?
Use DateTime.ParseExact and pass the format you have in date string. The current culture might have date form in which day comes before month like dd/mm/yy.
You are passing the same variable to ParseExact method that you have declared for DateTime. Change the name of DateTime object.
DateTime dtStartDate = DateTime.ParseExact(startDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
You can read the article from Custom Date and Time Format Strings to get more understanding for declaring the format for parsing the date string.
DateTime startDate = DateTime.ParseExact(strDate, "G", CultureInfo.InvariantCulture);
You can find more info on this here.
It looks like you are passing in your variable to parse before you have initialised it.
So you could have:
DateTime startDate = DateTime.Parse("05/30/2014 12:00:00");
Console.WriteLine(startDate);
Or, you could have:
String dateToParse = "05/30/2014 12:00:00";
DateTime startDate = DateTime.Parse(dateToParse);
Console.WriteLine(startDate);
Store your date inside a string variable and then use following:
DateTime.Parse(STRINGVARIABLE);
//Which means following
//DateTime.Parse("05/30/2014 12:00:00");
In your code, you are trying apply the format to the variable of type DateTime. Or if you want to apply the format directly to DateTime variable then you can simply write following:
DateTime.Parse("05/30/2014 12:00:00").ToString("dd MMM yyyy");