473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DateTime.ToString

Hi,

I have a problem with my Date conversion and can't find a way to resolve
it.

Here's my code :
friday= DateTime.Now.AddDays((day - (day * 2 + 2)))
thursday= friday.AddDays(6)
friday= friday.ToString("d-M-yyyy")
thursday= thursday.ToString("d-M-yyyy")

friday = #7/9/2004 9:51:25 AM#
thursday = #7/15/2004 9:51:25 AM#

After the conversion friday become 9/7/2004 but whit the thursday
conversion i receive this error : System.InvalidCastException:
Cast from string "15-7-2004" to type 'Date' is not valid.

I can't find a way to make it work, maybe the conversion think that 15
is the month instead of the day, i don't know...

Thanks.
Nov 18 '05 #1
2 1383
Andre,

The problem isn't with the date itself but with the container you are
putting it into.

Instead of re-using the "thursday" object you have already tacitly dimmed as
a date object (which is now trying to hold a string and thus the error) put
the converted date into a new variable:

Dim MyDateString As String = thursday.ToString("d-M-yyyy")

It is a much recommended practice to always define all of your objects
explicitly. You really should declare "Option Explicit" And "Option Strict"
at the beginning of your code. It will save you a lot of headache in the
long run.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Andre" <lo*****@cablevision.qc.ca> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
Hi,

I have a problem with my Date conversion and can't find a way to resolve it.

Here's my code :
friday= DateTime.Now.AddDays((day - (day * 2 + 2)))
thursday= friday.AddDays(6)
friday= friday.ToString("d-M-yyyy")
thursday= thursday.ToString("d-M-yyyy")

friday = #7/9/2004 9:51:25 AM#
thursday = #7/15/2004 9:51:25 AM#

After the conversion friday become 9/7/2004 but whit the thursday
conversion i receive this error : System.InvalidCastException:
Cast from string "15-7-2004" to type 'Date' is not valid.

I can't find a way to make it work, maybe the conversion think that 15
is the month instead of the day, i don't know...

Thanks.

Nov 18 '05 #2
Thank you. My problem is solved !

"S. Justin Gengo" <sjgengo@aboutfortunate[no-spam].com> wrote in message
news:10*************@corp.supernews.com...
Andre,

The problem isn't with the date itself but with the container you are
putting it into.

Instead of re-using the "thursday" object you have already tacitly dimmed as a date object (which is now trying to hold a string and thus the error) put the converted date into a new variable:

Dim MyDateString As String = thursday.ToString("d-M-yyyy")

It is a much recommended practice to always define all of your objects
explicitly. You really should declare "Option Explicit" And "Option Strict" at the beginning of your code. It will save you a lot of headache in the
long run.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Andre" <lo*****@cablevision.qc.ca> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
Hi,

I have a problem with my Date conversion and can't find a way to

resolve
it.

Here's my code :
friday= DateTime.Now.AddDays((day - (day * 2 + 2)))
thursday= friday.AddDays(6)
friday= friday.ToString("d-M-yyyy")
thursday= thursday.ToString("d-M-yyyy")

friday = #7/9/2004 9:51:25 AM#
thursday = #7/15/2004 9:51:25 AM#

After the conversion friday become 9/7/2004 but whit the thursday
conversion i receive this error : System.InvalidCastException:
Cast from string "15-7-2004" to type 'Date' is not valid.

I can't find a way to make it work, maybe the conversion think that 15 is the month instead of the day, i don't know...

Thanks.


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Misiak | last post by:
Hello- I live in the US, however I'm attempting to make a product of mine more international-friendly. There are a number of instances where a calendar function of my program displays various...
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
7
by: .Net Sports | last post by:
Before processing my data in a datagrid, I need to parse the day of the week (which will be my 'rqsday' variable) from a string that comes over on a querystring: string rqs =...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
5
by: ns21 | last post by:
Our application is windows desktop application. We are using VS.Net 2003, C#, Framework 1.1, SQL 2000. We use webservices to add/update/select objects. We are using XML Serialization. Following is...
6
by: Jake K | last post by:
string var = DateTime.Today.Month.ToString() + "-" + DateTime.Today.Day.ToString() + "-" + DateTime.Today.Year.ToString() + "-" + DateTime.Today.Hour.ToString() + "-" +...
4
by: Bill Gower | last post by:
Why won't this work? What do I need to do to make it work? DateTime? DateMember; if((DateTime.Parse(oldRow.ToString) == null)) DateMember = null; else DateMember =...
6
by: Manikandan | last post by:
Hi, I need to insert the datetime with milliseconds value into a datarow. My code as below DataTable testDataTable=new DataTable(); testDataTable.Columns.Add("updatedDateTime",...
4
by: vbcsdotnet | last post by:
hello I run an sql which returns a datarow itemarray field of system.datetime. the problem is the database only stores the date, but the returned value includes a time of 12:00:00 AM. I do not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.