This question already has answers here:
Converting a String to DateTime
(17 answers)
Parse a string containing date and time in a custom format
(3 answers)
Closed 4 years ago.
I have a string: "20180830" which represents 30 august 2018
I want to go to string: "30/08/2018"
So that I can do: DateTime parsedDate = DateTime.Parse("30/08/2018"); and have a DateTime instead of a string,
Tried everything but didn't succeed.
Needs some help.
You can use the DateTime.ParseExact-Method to solve your problem. Therefore you need to specify the exact format which would be yyyyMMdd in your case. Also the documentation suggests to use CultureInfo.InvariantCulture.
The following code...
DateTime datetime = DateTime.ParseExact("20180830", "yyyyMMdd", CultureInfo.InvariantCulture);
...should do the trick ;-)
Related
This question already has answers here:
How do I convert an Excel serial date number to a .NET DateTime?
(4 answers)
Closed 4 years ago.
I am trying to parse a integer(like 43392) to a date time.
The code looks like this:
DateTime d = DateTime.ParseExact(item[11], "dd/MM/yyyy", CultureInfo.InvariantCulture);
wher item[11] is "43392".
This throws an System.FormatException error, string was not recognized as a valid Datetime.
Need some guidance on this.
This should work
DateTime dateTime = DateTime.FromOADate(43392);
Output
19/10/2018 12:00:00 AM
DateTime.FromOADate Method (Double)
Returns a DateTime equivalent to the specified OLE Automation Date.
This question already has answers here:
.net: How to parse a date with this format: 08 Feb 2011 06:46
(3 answers)
Closed 4 years ago.
That question may be already been answered here, but I could not find any answers.
I'm trying to to convert the string 25APR18 to 2018-04-25.
I tried to do the following:
DateTime.ParseExact("25APR18", "yyyy-mm-dd", CultureInfo.InvariantCulture);
But it does not convert and gives me the error saying "String was not recognized as a valid DateTime."
I was not able to find the right solution.
Any suggestions?
You can use:
DateTime date = DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);
The string format is case sensitive representing the input format you want to match. Lower case mm represents minutes but you need month abbreviated name MMM and that's one issue with the code you posted. Full list of format specifiers can be found here.
Once you have a DateTime object you can retrieve the date as string with new format:
string formattedDate = date.ToString("yyyy-MM-dd");
Which produces:
2018-04-25
Your date parse code should be
DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);
This question already has answers here:
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 6 years ago.
I want to parse Datetime.Now into formatted string like
"2016-09-01T02:00:12.7011585Z"
You can't parse DateTime.Now. It already returns a DateTime, not a string.
You can generate it's string string representation using yyyy-MM-dd'T'HH:mm:ss.fffffff'Z' format with a proper culture like InvariantCulture;
DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'", CultureInfo.InvariantCulture)
This question already has answers here:
Creating DateTime object from string
(4 answers)
Closed 7 years ago.
I have a lot of data with timestamps in the following format: 20150603005845. I need to parse this and turn it into 2015/06/03 00:58:45 AM. I know I can convert it into a string and break it up and parse it the hard way but I was wondering if there are any inbuilt classes that can help me achieve this. I'm new to C# and don't know if there are any methods that can help me do this. Any help will be appreciated!
Parse date:
var date = DateTime.ParseExact("20150603005845", "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
Format date:
string formattedDate = date.ToString("yyyy/MM/dd HH:mm:ss tt");
This question already has answers here:
format date in c#
(7 answers)
Closed 8 years ago.
i have this string "1/20/2015 12:00:00 AM" and i need to convert in something like this "20/1/2015" without time and days first.
DateTime mydate = Convert.ToDateTime(myDateFromDB);
To convert date string of format "dd/MM/yyyy" to DateTime, should used DateTime.TryParseExact where you can specify the date format string to let the converter know about the Culture.
To get the Date alone, use myDateTimeObj.Date.