Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PARSING DATE AND TIME 1

Status
Not open for further replies.

gsc123

Programmer
Jan 24, 2008
197
I is it possible parse this time and date inot its coresponding bits?

8/6/2009 21:15:58

ie:
8 = day
6 = mth

...and so on, tried ages and now baffled
 
I don't understand how someone can work on something for so long, and not really make any progress. <sigh>

Anyway.... Use the DatePart function.... Like this...

Code:
<%
  pEndDate = cdate("8/6/2009 21:15:58")
  Response.Write "Year = " & DatePart("yyyy", pEndDate) & "<br />"
  Response.Write "Month = " & DatePart("m", pEndDate) & "<br />"
  Response.Write "Day = " & DatePart("d", pEndDate) & "<br />"
  Response.Write "Hour = " & DatePart("h", pEndDate) & "<br />"
  Response.Write "Minute = " & DatePart("n", pEndDate) & "<br />"
  Response.Write "Second = " & DatePart("s", pEndDate) & "<br />"
%>


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Alternate method:

<%
strDate = cdate("8/6/2009 21:15:58")
response.write "Month: " & month(strDate) & "<br />"
response.write "Day: " & day(strDate) & "<br />"
response.write "Year: " & year(strDate) & "<br />"
response.write "Hours: " & hour(strDate) & "<br />"
response.write "Minute: " & minute(strDate) & "<br />"
response.write "Second: " & second(strDate) & "<br />"
%>

gsc123,
Here's some reading material on dates:


it's hard to imagine someone who calls themselves a programmer can't solve the simplest tasks, like some of your questions in the past regarding how to write "IF" and "FOR" statements...



--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Thats great - so I cant now use same idea to subtract the No() date and time from this?
 
Sorry, using the Now() and doing subraction is not working - I'm trying to countdown from now to the EndDate - how do I know which way is subtraction because I'm getting minus figures...

SIGH
 
Try this:

Code:
<%
	
	pEndDate = cdate("6/7/2009 21:15:58")
	pDuration = cDate(pEndDate - Now())

	Response.Write "Time Remaining: "
	If Int(pEndDate - Now()) = 1 Then
		strTimeRemaining = cStr(Int(pEndDate - Now())) & " day "
	ElseIf Int(pEndDate - Now()) > 1 Then
		strTimeRemaining = cStr(Int(pEndDate - Now())) & " days "
	Else
		strTimeRemaining = ""
	End If

	If DatePart("h", pDuration) = 0 Then
	ElseIf DatePart("h", pDuration) = 1 Then
		strTimeRemaining = strTimeRemaining & " 1 Hour"
	Else
		strTimeRemaining = strTimeRemaining & " " & cStr(DatePart("h", pDuration)) & " Hours"
	End If
	
	If DatePart("n", pDuration) = 0 Then
	ElseIf DatePart("n", pDuration) = 1 Then
		strTimeRemaining = strTimeRemaining & " 1 Minute"
	Else
		strTimeRemaining = strTimeRemaining & " " & cStr(DatePart("n", pDuration)) & " Minutes"
	End If

	If DatePart("s", pDuration) = 0 Then
	ElseIf DatePart("s", pDuration) = 1 Then
		strTimeRemaining = strTimeRemaining & " 1 Second"
	Else
		strTimeRemaining = strTimeRemaining & " " & cStr(DatePart("s", pDuration)) & " Seconds"
	End If

	Response.Write strTimeRemaining
	
%>


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Bingo! - I've been up to long - thank you
 
After deducting now time from end time - what algorithm should I use to deduct if the time is up?
 
Pseudo-code

Code:
If pEndDate < Now() Then
  ' You are out of time
Else
  ' you have time left
End If


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I see - well my database has these back to front thats why thxs
 
Hopefully you are storing your dates in a DateTime column. If you are, and you are not "Messing" around with the values when you return them, then they should still be DateTime data in ASP. Specifically.... in ASP.... they should be a DATE date type. (The Date data type has both date and time).

One of the developers that works for me was having a similar problem today. My advice to him was.... when dealing with date and time, keep the data in a Date data type as much as possible. Believe it or not, the folks that wrote ASP spent a lot of time and energy making sure that Date operations work properly. Once you start messing around with strings... all bets are off.

well my database has these back to front thats why

You should understand a little about how dates are stored. I'm assuming you are using a SQL Server database. In SQL Server, dates are stored as a pair of integers.


When you say that the database has them back to front, that's not really true. When you select a date from the database, your application recognizes that it's a date, and then it chooses to display them a certain way. For example, SQL Server Management Studio (and Query Analyzer) will display dates like this:
2009-06-03 19:07:23.763

But... you need to realize that this is just a display of the actual data, not the data itself. Nobody would be able to make sense of dates if SQL Server displayed them as a pair of integers.

Anyway... my point is. Treat your dates as though they are dates, and you shouldn't have too many problems. If you have a string (that represents a date) that you want to treat as a date, then you should use the cDate function. Like this:

pEndDate = cdate("6/7/2009 21:15:58")

Even better is use the DateSerial And TimeSerial functions (if you can). Example:

pEndDate = DateSerial(2009,6,7) + TimeSerial(21,15,58)

If you are retrieving date data from a database, then you should use the cDate function. Ex:

pEndDate = cDate(RS("MyDateColumn"))

In ASP, all variables are actually variants (they can take on any data type). But... variants have sub types too. So.... if your variable is a string, then string handling functions apply. If the sub-type of the variable is a date, then date handling applies.

For example, if you do this...

Code:
	MyDate = "6/4/2009 13:15:58"
	Response.Write MyDate + 1

You will get an error because you are trying to add 1 to string. But, if you do this....

Code:
	MyDate = cDate("6/4/2009 13:15:58")
	Response.Write MyDate + 1

You will get an output like this: "6/5/2009 13:15:58"
The cDate function set the variables sub-type to Date. Adding 1 to a date adds a day (so you get June 5 instead of the original June 4).

These are very important concepts to understand. Please make sure all of this makes sense to you. If it doesn't, please feel free to ask followup questions.


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I see so I could you this concept to add days to a db?

CODE
MyDate = cDate("6/4/2009 13:15:58")
some varDate = MyDate + 1

update my table set date ='"&varDate&"' where something equals = lahlah

....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top