473,387 Members | 1,420 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,387 software developers and data experts.

string to date

cj
This works:

If remoteFileInfo(arrayIndex).m_strFileDate < Now.AddDays(-25) Then ....

but should it???

remoteFileInfo(arrayIndex).m_strFileDate is a string for example:
"9/6/2007 7:00:00 AM"

Now.AddDays(-25) is a date for example:
#8/12/2007 10:52:56 AM#

Shouldn't I be converting remoteFileInfo(arrayIndex).m_strFileDate to a
date before compairing?
Sep 6 '07 #1
8 2020
This works:
>
If remoteFileInfo(arrayIndex).m_strFileDate < Now.AddDays(-25) Then
....

but should it???
I'm nopt in a position to test this right now but I would have to guess something
like.

You are not using "option Strict on"
VB.net may be converting your date to a strng for comparison purposes

This means that you are likely getting lucky for now.

I would suggest
----------------------------------------------------
If Convert.ToDateTime(remoteFileInfo(arrayIndex).m_st rFileDate) < Now.AddDays(-25)
Then
----------------------------------------------------

--
Rory
Sep 6 '07 #2
cj
Found it.

If cdate(remoteFileInfo(arrayIndex).m_strFileDate < Now.AddDays(-25)

is what I was looking for.

P.S. I like option strict off but it was bugging me that I couldn't
remember how to convert a string to a date. All I could think of was
stod() which is VFP.

Rory Becker wrote:
>This works:

If remoteFileInfo(arrayIndex).m_strFileDate < Now.AddDays(-25) Then
....

but should it???

I'm nopt in a position to test this right now but I would have to guess
something like.

You are not using "option Strict on" VB.net may be converting your date
to a strng for comparison purposes

This means that you are likely getting lucky for now.

I would suggest
----------------------------------------------------
If Convert.ToDateTime(remoteFileInfo(arrayIndex).m_st rFileDate) <
Now.AddDays(-25) Then
----------------------------------------------------

--
Rory

Sep 6 '07 #3
cj wrote:
This works:

If remoteFileInfo(arrayIndex).m_strFileDate < Now.AddDays(-25) Then ....

but should it???
No, it doesn't work. It might compile, but it doesn't work. The date
will be converted to a string, and comparing dates as strings doesn't
work unless you use the ISO 8601 format.
remoteFileInfo(arrayIndex).m_strFileDate is a string for example:
"9/6/2007 7:00:00 AM"

Now.AddDays(-25) is a date for example:
#8/12/2007 10:52:56 AM#

Shouldn't I be converting remoteFileInfo(arrayIndex).m_strFileDate to a
date before compairing?
Yes. Use the Convert.ToDateTime method.

--
Göran Andersson
_____
http://www.guffa.com
Sep 6 '07 #4
cj
Yes, it did work. Can't say why, but it did. I was looking for cdate()

Göran Andersson wrote:
cj wrote:
>This works:

If remoteFileInfo(arrayIndex).m_strFileDate < Now.AddDays(-25) Then ....

but should it???

No, it doesn't work. It might compile, but it doesn't work. The date
will be converted to a string, and comparing dates as strings doesn't
work unless you use the ISO 8601 format.
>remoteFileInfo(arrayIndex).m_strFileDate is a string for example:
"9/6/2007 7:00:00 AM"

Now.AddDays(-25) is a date for example:
#8/12/2007 10:52:56 AM#

Shouldn't I be converting remoteFileInfo(arrayIndex).m_strFileDate to
a date before compairing?

Yes. Use the Convert.ToDateTime method.
Sep 6 '07 #5
cj wrote:
Yes, it did work. Can't say why, but it did.
I see. Yes, actually it does work in this case. For some reason it
converts the string to a date, not the date to a string.

Relying on that the compiler manages to correctly guess what you mean is
not a good practice, though. You should use "Option Script On" so that
you don't do conversions like this by mistake.

If you don't know why something works, you can't know if it's actually
working correctly for all possible cases, or if it's going to work in
the future. Just because something looks like it works doesn't
automatically mean that it's correct. I've seen code that has been used
for years, that suddenly failed just because it was wrong from the
beginning.

--
Göran Andersson
_____
http://www.guffa.com
Sep 6 '07 #6
cj


Göran Andersson wrote:
cj wrote:
>Yes, it did work. Can't say why, but it did.

I see. Yes, actually it does work in this case. For some reason it
converts the string to a date, not the date to a string.

Relying on that the compiler manages to correctly guess what you mean is
not a good practice, though. You should use "Option Script On" so that
you don't do conversions like this by mistake.
IYHO of course :)
>
If you don't know why something works, you can't know if it's actually
working correctly for all possible cases, or if it's going to work in
the future. Just because something looks like it works doesn't
automatically mean that it's correct. I've seen code that has been used
for years, that suddenly failed just because it was wrong from the
beginning.
Sep 7 '07 #7
cj wrote:
>>
Relying on that the compiler manages to correctly guess what you mean
is not a good practice, though. You should use "Option Script On" so
that you don't do conversions like this by mistake.
IYHO of course :)
I'd *think* you'll find that *most* people (I wonder how much trouble I can
get in for using that phrase wthout actually asking most people ;P) would
agree with Goran.

Codeing VB.net without "Option Strict On" as a *default* stance can be much
like the difference between coding VBA and Coding VB6

If there is a possibility that the compiler could get it wrong, then you
shouldn't leave it up to the compiler. you should tell the compiler explicitly.
"Option Strict on" is a great way to get the compiler to tell you where the
uncertainty is.

That said there are places where the reverse is true,. It's just that there
are not many so IMHO (:)) the deafult should be "Option strict On"


--
Rory
Sep 7 '07 #8
I'd *think* you'll find that *most* people (I wonder how much trouble I can
get in for using that phrase wthout actually asking most people ;P) would
agree with Goran.
Don't worry Rory - if you, Goran, and I all agree about Option Strict
On then it has to be right. After all we're know better than *most*
people (hehe just kidding - no flames please :-))

It's true though - I see no reason why any developer would leave this
off, unless they have to use late-binding (though I believe you could
just suppress the message). The annoyance of having to explicitly cast/
convert all your types is nothing compared to having to explain to a
client that that phantom crash they keep having is because of you
forgot to cast a string into a integer before multiplying. Besides, if
you ever plan on using a different language (say C#) you need to get
used to explicitly declaring everything, as C# is more picky than VB
with Option Strict On.

After all, the old saying is "Never put off until runtime what you can
fix at compile time."

Or something like that...

Thanks,

Seth Rowe
Sep 7 '07 #9

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

Similar topics

2
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a...
3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
6
by: Thomas Scheiderich | last post by:
I have the following page as test.aspx: *************************************************** <html> <head> <title>Hello and Welcome Page</title> </head> <body> <center> <% Dim CurrentDate As...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
3
by: carl.barrett | last post by:
Hi, I have a number of buttons on a form which run mailmerges. Next to each button is a text box/control that the user enters a date into when the letter was created/merged. When the user...
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
22
by: Simon Says | last post by:
Dear all, I have the following: dim tmpString as String = "11/21/2004 07:55:33" dim tmpDate as Date = CDate(tmpString) But I keep getting <cast from string "11/21/2004 07:55:33" to type...
2
by: Kun | last post by:
I have an html form that takes dates and inserts them into a mysql file. Currently, users have to type in dates in the yyyy-mm-dd format. As of now, this process works with the sql. However, I...
1
by: jwf | last post by:
This question continues on from a previous post "DATE to string" but I think it deserves a new thread. In my previous post I was trying to convert a DATE to string in a NON MFC C++ application...
5
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.