C#, datetime.parse Error 32510 can not parse - c#

everyone.
I have a datetime coming back from a music file in the format:
32510
Maybe, it means 1989/01/02
How can i get the datetime.parse function to pick up on this? Ie parse it without erroring? Cheers

This DateTime(32510) is in double so we can't simply use datetime.parse to convert 32510 to DateTime. To convert a double to date we need to use DateTime.FromOADate method.
For more details you can go through this MSDN link:
https://msdn.microsoft.com/en-us/library/system.datetime.fromoadate(v=vs.110).aspx

Quite often interop dates are stored as integers, so you'll need to convert them to actual dates. See below as an example:
var dt = DateTime.FromOADate(32510);
Console.WriteLine(dt);
Where the output is:
2/01/1989 12:00:00 AM
I don't think you can use DateTime.Parse() to convert 32510 to 1989/01/02 though.

It sounds me like Ole Date, try using DateTime.FromOADate
DateTime date = DateTime.FromOADate(32510)
//output - 1/2/1989 12:00:00 AM

Related

Convert to datetime from Oracle

i know there are a lot of similar questions, but I couldn't find what I was looking for.
Here is my oracle date:
string testdate= "2014-01-07 15:00:00.0000000";
And here is how I tried to convert to datetime:
DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)
This throws a format exception. Any ideas?
My quick test also throws the string not valid datetime exception. Quick test:
Console.WriteLine(DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture).ToShortDateString());
I'd start by trying to avoid getting it as a string in the first place. Make sure you're using the appropriate data type in Oracle, and you should be able to call GetDateTime on the appropriate DataReader (or whatever you're using).
If you must parse it as text, then you need to specify a format which matches the value - so use 7 fs instead of 3, given that your value has ".0000000" at the end.
DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fffffff",
CultureInfo.InvariantCulture)
But again, I'd strongly urge you to avoid having to deal with the value as text at all.
Why use ParseExact at all? Reqular Parse seems to work.
var dt = DateTime.Parse("2014-01-07 15:00:00.0000000", CultureInfo.InvariantCulture);
// Prints out 2014-01-07T15:00:00.0000000
Console.WriteLine(dt.ToString("o"));

SQL datetime to C# string and back to SQL datetime

I have a webservice method that gets data from sql of the format
2012-11-18 11:21:03 when i save it to C# string it becomes this format: 18.11.2012 11:21:03
How do i change it back to the SQL format 2012-11-18 11:21:03 ?
Parse it into a dateTime again
DateTime myTime = DateTime.Parse(myString);
and back into a proper to string
myTime.ToString("yyyy-MM-dd HH:mm:ss");
Or just read it into a datetime and cut out the middleman.
You can get the universally sortable string format (which looks like the one used by SQL server) by using the format string "u" like this:
var dateTimeString = String.Format("{0:u}", yourDateTime);
Simply run the below code,
var newDateTime = oldDateTime.Date.ToString("yyyy-MM-dd HH:mm:ss");
Its just converting it back to the SQL Format DATETIME
Trouble with Dates as strings is they are ambiguous and the formats can vary based on where you are in the world, or even local machine settings. You might assume a date string is yyyy-mm-dd but what if it is actually yyyy-dd-mm? Some dates will appear to work and some will be invalid.
In other words is 2013-02-10 the 10th of February or is it the 2nd of October? If it is just a string you have no way of knowing for sure what was intended.
Your best bet as suggested by #Haedrian is to store in a DateTime C# object, not a string. That way it is never ambiguous and you have access to various date specific functions. If you must store as a string you can convert back to a date as above or use
DateTime.TryParse(datestring, out dateVariable);
which won't throw an exception for an invalid format. Depends if you want exceptions!
Also I would suggest if you must use strings to use a 3 character month in strings, which again eliminates the ambiguity, e.g.
"dd-MMM-yy hh:mm tt"

how to conver date string from one format to another format in c#?

Suppose I have date string like mydate = "24-Jun-2011";
I want to convert it to another format "2011-06-24".
What is the simple way to do this?
The best way is to parse the string to a DateTime and then convert it to a string again.
Be sure to have a look at the documentation for DateTime.Parse, DateTime.TryParse and DateTime.ToString
DateTime.Parse(myDate).ToString("yyyy-MM-dd");
DateTime.ParseExact("24-Jun-2011", "dd-MMM-yyyy").ToString ("yyyy-MM-dd")
See formats here at MSDN.
U can Parse it to DateTime and then using tostring + special format get what u need
http://www.csharp-examples.net/string-format-datetime/
has a lot of different formatting options... This should work well for you.

Convert DateTime to string with format YYYYMMDD

I have birth dates stored as datetime in SQL Server 2008 like so:
2010-04-25 00:00:00.000
What is the best way, using C#, to convert and format this into a string with a YYYYMMDD format?
In the end, all I need is a string like:
20100425
Any help is greatly appreciated!
date.ToString("yyyyMMdd");
Should be what you need.
You need to get that value into a DateTime object and then you can use it's ToString() function like so:
.ToString("yyyyMMdd");
Are you able to get the data out of the database as a DateTime (.NET) object? If so, you can use the DateTime's instancename.ToString("yyyyMMdd")
If you haven't gotten to that stage yet, there's quite a few different ways to get the data out. It's a whole Google search in itself...
You just format the date using a custom format string:
string formatted = theDate.ToString("yyyyMMdd");
Note that the date doens't have a format at all when it's stored as a datetime in the database. It's just a point in time, it doesn't have a specific text representation until it's specifically created from the date.
Use the .ToString() method on the date time object, and pass in the format you want.

Problem during parsing datetime

I have problem when im trying parse datetime in format like: "1.00:29:00" 1- days,29-minutes, after invoke DateTime.Parse im getting "String was not recognized as a valid DateTime"
thanks in advance for any suggestion.
That's not a valid native datetime string format - see the remarks section here for more info - but that sounds a lot like you're really talking about a TimeSpan.
You can use ParseExact providing the format to use along with the value to parse.

Categories