473,385 Members | 2,015 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,385 software developers and data experts.

DateTime short formatted range lookup

Hello,

I'm trying to figure out a method to look up by a range of dates,
entries in a database. The format of the date in the database is "M\D
\yyyy HH:MM:SS".

What i need to do is take the dates that are in the database
(formatted as above), isolate the date (which is in short format) and
compare it to the short formatted date in a datetimepicker.

How can I take a string and format it as a short date?

How can I compare two short dates, to find if the date falls in the
date range? (isolate each part of the date? is there an actual
function that handles this?)
Any help is greatly appreciated,

Matt

Aug 7 '07 #1
3 2215
Matt Brown - identify wrote:
Hello,

I'm trying to figure out a method to look up by a range of dates,
entries in a database. The format of the date in the database is "M\D
\yyyy HH:MM:SS".
Why are you storing the date as a string in the database? Store it as a
date.
What i need to do is take the dates that are in the database
(formatted as above), isolate the date (which is in short format) and
compare it to the short formatted date in a datetimepicker.

How can I take a string and format it as a short date?
You can't. You first have to convert it to a date, then you can format
it to a string.
How can I compare two short dates, to find if the date falls in the
date range?
You can't. The date format that you are using is not comparable.
(isolate each part of the date? is there an actual
function that handles this?)
The DateTime structure has a Date property that gives you the date part,
i.e. a DateTime value where the time part is 00:00:00.

However, you don't need any of that for what you are doing. Get the date
from the DateTimePicker, and calculate the starting and ending time of
the interval. Then you can easily select the records from the database
where the date is between them.

That is of course if you change the database so that you store the date
as a date, so that it's possible to compare it to anything at all.

--
Göran Andersson
_____
http://www.guffa.com
Aug 8 '07 #2
On Aug 7, 9:43 pm, Göran Andersson <gu...@guffa.comwrote:
>
Why are you storing the date as a string in the database? Store it as a
date.

It is stored as datetime.
>
You can't. You first have to convert it to a date, then you can format
it to a string.
I don't need to convert it.
>
You can't. The date format that you are using is not comparable.
Are they both short dates?
1) M/D/yyyy
2) M/D/yyyy HH:MM:SS

If not, there must be a conversion?
>
The DateTime structure has a Date property that gives you the date part,
i.e. a DateTime value where the time part is 00:00:00.

However, you don't need any of that for what you are doing. Get the date
from the DateTimePicker, and calculate the starting and ending time of
the interval. Then you can easily select the records from the database
where the date is between them.

That is of course if you change the database so that you store the date
as a date, so that it's possible to compare it to anything at all.

Can you give me some syntax or examples? for instance, if the date
time pickers are called dateStart and dateEnd, how would i compare the
two?

I'm guessing that the variable that I'm setting the date value out of
the datebase to, needs to be a datetime?

I'm new at this.
Thanks!

Matt
Aug 8 '07 #3
Matt Brown - identify wrote:
On Aug 7, 9:43 pm, Göran Andersson <gu...@guffa.comwrote:
>Why are you storing the date as a string in the database? Store it as a
date.


It is stored as datetime.
You said that the date had a specific format, that's why I assumed that
it was a string. A datetime value doesn't have a format.
>You can't. You first have to convert it to a date, then you can format
it to a string.
I don't need to convert it.
>You can't. The date format that you are using is not comparable.

Are they both short dates?
1) M/D/yyyy
2) M/D/yyyy HH:MM:SS
These are date format, not dates. DateTime values doesn't have a format,
they only get a format when you convert them to strings. A DateTime
value always contains a date and a time component.
If not, there must be a conversion?
If you convert the dates into strings in that format, you can't compare
them.
>The DateTime structure has a Date property that gives you the date part,
i.e. a DateTime value where the time part is 00:00:00.

However, you don't need any of that for what you are doing. Get the date
from the DateTimePicker, and calculate the starting and ending time of
the interval. Then you can easily select the records from the database
where the date is between them.

That is of course if you change the database so that you store the date
as a date, so that it's possible to compare it to anything at all.


Can you give me some syntax or examples? for instance, if the date
time pickers are called dateStart and dateEnd, how would i compare the
two?
You do like this in the SQL query:

.... where DateFieldInDataBase >= @Start and DateFieldInDateBase < @End

@Start and @End are parameters. You add Parameter objects to the Command
object with the values from dateStart and dateEnd.

If dateEnd is the date of the last day, you use dateEnd.AddDays(1) to
get the end time.
I'm guessing that the variable that I'm setting the date value out of
the datebase to, needs to be a datetime?
Yes.

--
Göran Andersson
_____
http://www.guffa.com
Aug 8 '07 #4

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

Similar topics

99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
5
by: _Andy_ | last post by:
I'm looking for the alogithm to take a piece of 8-bit audio data, and scale it to a 16-bit short. I got as far as: private static short ByteToShort(byte b) { int word = 0; word |= ( ( b &...
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" ...
4
by: darrel | last post by:
I'm binding fileinformation to a repeater and attempting to display the 'lastWriteTime' property as a formatted date. This: <%# DataBinder.Eval(Container.DataItem, "LastWriteTime" %> Gives...
6
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a...
5
by: rbtmaxwell | last post by:
Hello, Not sure If I'm posting in the correct group here, so I appologize in advance if I selected the wrong one. I am trying to initialize a short with a hex value... sounds trivial right?...
7
by: TheLostLeaf | last post by:
DateTime tTime = DateTime.Now; ------------------------------------------------------------------------------------------- tTime returns "1:59:00 PM" it never returns seconds. Database field...
3
by: Tim | last post by:
Folks, I'm trying to format a print string so that it reports progress whilst processing a looping structure with a date time stamp appended to the end of the string. This started out life as a...
1
by: Magnus.Moraberg | last post by:
Hi, I have the following event which checks if a cell in a dgv contains a date and if so, checks if it is formatted correctly. I don't think its good practice for be to try this by catching an...
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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.