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.
Related
I get an error
String was not recognized as a valid TimeSpan
I am coding in C# and can't seem to find out what the problem is. How to avoid this error and is time(7) datatype in my database.
Any insight would be very much appreciated.
This is my code where it's failing:
Classes.ClsBooking.BookingDataTable.InsertBooking(TimeSpan.Parse(string.Format(DateTime.Now.ToString(), "HH:mm")));
Use either:
string.Format(DateTime.Now, "HH:mm")
or, preferably
DateTime.Now.ToString("HH:mm")
With what you are currently doing, you're passing a string (that's already formatted the date in the "default" format) as a parameter to "string.Format()", and then you're trying to tell "string.Format()" to reformat that string as "HH:mm". That won't work, as the parameter will no longer be recognised as DateTime
you just want TimeSpan.Parse(DateTime.Now.ToString("HH:mm"));
This could be quite a straightforward question, I've tried find a answer but couldn't.
What I'm trying to do is to format my DateTimeOffset to use format specifier "G" and append timezone "zzz" with it. I like to use 'CurrentCulture' as well.
myDateTimeOffset.ToString("G zzz", CultureInfo.CurrentCulture)
However, I'm getting result like 'G +12:00'.
I'm expecting to get like '28/07/2016 3:36:31 PM +12'.
Any advice to get it to format properly? Thanks.
try the following:
string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:G} {0:zzz}", myDateTimeOffset);
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
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.
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.