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.
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 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'm very new to C# and I think I got something on my mind but can't quite figure it out.
So I got this school project that needs to ask the first name, last name and date of birth of the user but with certain limitations. I can figure the names out but the date problem persists.
The date of birth should be in DD.MM.YYYY. format and have certain limitations:
Date should be between 01-31
Month between 01-12
Year between 1900-2050
I can get it to ask but it won't specify the format and I don't know what variable to use.
To parse dates in custom format, DateTime.TryParse method is best fit.
You just need to find culture, which uses your date format, for example "fr-CH".
Then you use mentioned method to check if format of date was correct. It automatically checks if date is vaild, i.e. month is between 1 and 12, day of month is in correct range (1 through 28,29,30 or 31 depending on month and year).
You just need to additionally check the year.
Try this code (I used short-circuiting operator &&, so if parsing was successfull, then check the year):
DateTime dt;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-CH");
DateTimeStyles styles = DateTimeStyles.None;
if(DateTime.TryParse("28.01.2018", culture, styles, out dt)
&& dt.Year >= 1900 && dt.Year <= 2050) // here you check additionally if year is in correct range
Console.WriteLine("Date successfully parsed!");
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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am getting GMT DateTime as string input. For example
SampleDate = "20170221T085258.732 GMT"
Now, I want to convert this to datetime object. What is the best way of doing this conversion?
You just need to use ToLocalTime() Then you can change it to whatever timezone you care about.
DateTimeOffset.Parse(SampleDate).ToLocalTime();
var offset = new Date().getTimezoneOffset();
To remove the GMT and time zone, change the following line:
document.write(d.toString().replace(/GMT.*/g,""));
Hi try this code using DateTime.ParseExact()
string SampleDate=""20170221T085258.732 GMT";
DateTime dateObject = DateTime.ParseExact(SampleDate,"ddd MMM dd yyyy HH:mm:ss 'GMT'zzz", System.Globalization.CultureInfo.InvariantCulture);
For more info heres the link for DateTime.ParseExact MSDN: https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
Below code worked for me. Date contain some unwanted characters like "T",".","GMT" , once I removed those, it started working..
But I feel that, there has to be some better solution for this.
//I can write a regular expression to keep only numeric values and avoid this replacements...
SampleDate = "20170221T085258.732 GMT"
SampleDate = SampleDate.Replace("GMT", "")
SampleDate = SampleDate.Replace("T", "")
SampleDate = SampleDate.Replace(".", "")
Dim dateObject As DateTime = DateTime.ParseExact(SampleDate.Trim(), "yyyyMMddHHmmssfff", System.Globalization.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 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();