Parsing a integer using DateTime.ParseExact [duplicate] - c#

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.

Related

Why this double value cannot be converted [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 3 years ago.
In the above code, I am getting the date data from SpreadsheetDocument. The exampled date cannot be converted.
But other dates can be converted.
Why cant i convert this date and how can I convert it in different way?
DateTime.FromOADate(double.Parse("05.09.1977"));
I want to convert this string to DateTime with this.
"05.09.1977 is not a valid double. It looks like it's an actual date (either MONTH.DAY.YEAR or DAY.MONTH.YEAR).
To parse it to a DateTime use either:
DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture)
or
DateTime.ParseExact("05.09.1977", "MM.dd.yyyy", CultureInfo.InvariantCulture)
This way:
Console.WriteLine(DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture));
Output:
05/09/1977 00:00:00
according to the
DateTime.FromOADate(Double) method doc
Returns a DateTime equivalent to the specified OLE Automation Date.
public static DateTime FromOADate (double d);
Parameters
d
Double
An OLE Automation Date value.
so, you have to pass a double as argument
but double.Parse("05.09.1977")???
how can that be a double??
that is the reason

how to convert String 03/20/2019 10:46 to Date time in C#? [duplicate]

This question already has answers here:
DateTime.Parse throwing format exception
(3 answers)
Closed 3 years ago.
i have a string 03/20/2019 10:46 i want to convert to datetime 03/20/2019 10:46:00 AM
i have tried to use but failed as i am getting exception convert.todatetime("03/20/2019 10:46") but i am getting format exception.
new into development need help.really confused what need to be done. i have also saw other question regarding this topic on stckoverflow but failed i have also used
datetime.parse but still failed
You can use DateTime.ParseExact() method
var dt = DateTime.ParseExact("03/20/2019 10:46", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
Output:
3/20/2019 10:46:00 AM
POC : .netFiddle
try this:
string dateTime = "03/20/2019 10:46";
DateTime parsedExactTime = DateTime.ParseExact(dateTime, "MM/dd/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);
It´s missing the AM/PM though...

Convert string in to DateTime [duplicate]

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 ;-)

Cannot translate date in current locale [duplicate]

This question already has answers here:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
(13 answers)
Closed 5 years ago.
I'm trying to translate a Date in the current system language, this is my date: 2018/01/01, I tried to achieve my goal in this way:
var date = DateTime.ParseExact("2018/01/01", "dddd MMMM yyyy", new CultureInfo("it-IT")).ToString();
unfortunately I get:
System.ArgumentNullException
The InnerException say:
String not recognized as a valid DateTime value.
I used ParseExact to avoid this error, what I did wrong?
Use this code it will work.
var date = DateTime.ParseExact("2018/01/01", "yyyy/MM/dd", new CultureInfo("it-IT"));
I think you now know the error reason.
Thanks

how to convert date and time to date [duplicate]

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.

Categories

Resources