Input string was not in correct format

  • Thread starter Thread starter Paul Hale
  • Start date Start date
P

Paul Hale

I recently moved an asp.net project from win server 2003 \ IIS 6 \ Framework
2.0 to Win server 2008 \ IIS 7 \ Framework 3.5.

Now the following line of code returns an error “Input string was not in a
correct formatâ€
Dim TimeLeft As Date =
objPlayer.DateExpires.Subtract(TimeSpan.Parse(Now().ToLongTimeString))
Any idea why this is now broken?

Paul.
 
Why are you mixing datetime and timespan objects? You should just be using
something like this:

dim TimeLeft as TimeSpan
TimeLeft = objPlayer.DateExpires.Subtract(DateTime.Now)

I'm sorry, but I cannot answer why the code you provided worked before,
other than say I'm surprised it did.
 
Hi Paul,

In the code you provided, you use TimeSpan.Parse to get a timespan from a
string value, however the string value is returned from
"DateTime.Now().ToLongTimeString". I think this is error prone as
ToLongTimeString return the date/time format string which may not match a
TimeSpan format:

#TimeSpan..::.Parse Method
http://msdn2.microsoft.com/en-us/library/system.timespan.parse.aspx

Also, since the app used to work on development server but failed when
moving to a new server, I think the date/time regional setting is also a
potential cause(because the datetime output rely on the region setting).
You can check the two machines for the region setting.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
Thanks for your response.

I looked at the regional settings on the new server and everything was set
to English UK as expected.

On further investigation the problem was with either "AM" or "PM" being
appended to the time string.

I have resolved by using the very ugly code below (but it works)

Dim strNowTimeWithAMorPM As String = Now.ToLongTimeString()
Dim strNow As String = strNowTimeWithAMorPM.Remove
strNowTimeWithAMorPM.Length() - 3, 3)

Dim TimeLeft As Date = objPlayer.DateExpires.Subtract(TimeSpan.Parse(strNow))

Paul.
 
On further investigation the problem was with either "AM" or "PM" being
appended to the time string.

I have resolved by using the very ugly code below (but it works)

Dim strNowTimeWithAMorPM As String = Now.ToLongTimeString()
Dim strNow As String = strNowTimeWithAMorPM.Remove
strNowTimeWithAMorPM.Length() - 3, 3)

Dim TimeLeft As Date = objPlayer.DateExpires.Subtract(TimeSpan.Parse(strNow))


Instead of doing that, just use the ToString method and pass in the
exact format you want:

Dim TimeLeft As Date =
objPlayer.DateExpires.Subtract(TimeSpan.Parse(Now.ToString("ddMMyyyy"))
'Or whatever format you need

Chris
 
Thanks for Chris's input.

Hi Paul,

I agree with Chris that you can try explicitly format the output datetime
string to the format you expected(for timespan).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

--------------------
 
Back
Top