Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Formatting Data for Time


Reply
 
Thread Tools Display Modes
  #1  
Old 11-14-2005, 03:01 PM
jnt4evrb jnt4evrb is offline
Regular
 
Join Date: Oct 2005
Posts: 65
Default Formatting Data for Time


I have built an application to report my backup times on our servers. I have not formatted time before and am not sure how to display it.

I have declared my variables as Date and used the dte prefix on my date controls. (I know that it is just for my use.) but when I display the total in a label I am getting the date shown as mm/dd/yy. I need it shown in hours and minutes.

Here is an example of my code.

lblServerNameTotal.Caption = Format?(dteServerNameTotal)
Replace the ? for the format I need to use.

I know this is a breeze for you all but your help is greatly appreciated.

Thanks
Reply With Quote
  #2  
Old 11-14-2005, 03:20 PM
00100b's Avatar
00100b 00100b is offline
Martian In Disguise

Retired Moderator
* Guru *
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 9,566
Default

Code:
lblServerNameTotal.Caption = Format$(dteServerNameTotal, "mm/dd/yyyy hh:nn")
__________________
The only stupid question is the one that goes un-asked.
Reply With Quote
  #3  
Old 11-17-2005, 09:23 AM
jnt4evrb jnt4evrb is offline
Regular
 
Join Date: Oct 2005
Posts: 65
Default

I am still not able to get the time to display correctly. It is coming up 00:00.

Here is the code I have...

'Declare local level variables
Dim dteInfonet As Date
Dim dteInfonetStart As Date
Dim dteInfonetTotal As Date

'Convert input values as numeric values

dteInfonet = Val(txtInfonet.Text)

'Calculate Totals
dteInfonetTotal = dteInfonet - dteInfonetStart

'Display Totals in labels
lblInfonetTotal.Caption = Format$(dteInfonetTotal, "hh:mm")

What it looks like is


Server Backup Start Time Backup End Time Time needed for Backup
(dteInfonetStart) (dteInfonet) (dteInfonetTotal)
(lblInfonetStart.Caption) (txt.Infonet.Text) (lblInfonetTotal.Caption)

Infonet 22:30 22:35 00:00

The time needed for backup should be 00:05 but it is not.
Any ideas?
Reply With Quote
  #4  
Old 11-17-2005, 09:26 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

The problem is probably when you calculate the Difference. You should be using the DateDiff() function instead of just using subtraction.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #5  
Old 11-17-2005, 11:48 AM
jnt4evrb jnt4evrb is offline
Regular
 
Join Date: Oct 2005
Posts: 65
Default

I am not 100% use I understand where to use "DateDiff"

'Calculate Totals
dteInfonetTotal = dteInfonet - dteInfonetStart
dteInfonetTotal = dteInfonet DateDiff dteInfonetStart
or is it...
lblInfonetTotal.Caption = DateDiff(dteInfonetTotal, "hh:mm")

or am I wrong on both accounts?
Reply With Quote
  #6  
Old 11-17-2005, 11:55 AM
Volte's Avatar
Volte Volte is offline
Ultimate Contributor

Retired Leader
* Guru *
 
Join Date: Aug 2001
Posts: 5,343
Default

You're close, but DateDiff doesn't work that way. You need to specify two dates and an interval in the DateDiff() function. The interval refers to the type of quantity you want measured between the dates: days, weeks, months, etc.

Something like this should work:
Code:
Dim mins As Long Dim totalTime As String mins = DateDiff("m", dteInfonetStart, dteInfonet) totalTime = Format$(CLng(mins / 60) & ":" & mins Mod 60, "hh:mm")
Reply With Quote
  #7  
Old 11-17-2005, 12:07 PM
jnt4evrb jnt4evrb is offline
Regular
 
Join Date: Oct 2005
Posts: 65
Default

I declared...
Dim mins As Long
Dim totalTime As String
to my local variables

and added


'Display Totals in labels

lblInfonetTotal.Caption = Format$(dteInfonetTotal, "hh:mm")

mins = DateDiff("m", dteInfonetStart, dteInfonet)
totalTime = Format$(CLng(mins / 60) & ":" & mins Mod 60, "hh:mm")

and recieved 00:00 again.
I removed the

Format$(dteInfonetTotal, "hh:mm") and made it ...
Display Totals in labels

lblInfonetTotal.Caption = mins = DateDiff("m", dteInfonetStart, dteInfonet)
totalTime = Format$(CLng(mins / 60) & ":" & mins Mod 60, "hh:mm")
and recieved "False" in my label

I tried...
'Display Totals in labels

mins = DateDiff("m", dteInfonetStart, dteInfonet)
totalTime = Format$(CLng(mins / 60) & ":" & mins Mod 60, "hh:mm")
and recieved a blank in my label.

Save my hair and tell me what I am doing wrong.

Thanks!

Joe
Reply With Quote
  #8  
Old 11-17-2005, 12:36 PM
Volte's Avatar
Volte Volte is offline
Ultimate Contributor

Retired Leader
* Guru *
 
Join Date: Aug 2001
Posts: 5,343
Default

Once you set totalTime just set the label's caption = to totalTime. No further calculation is necessary.
Reply With Quote
  #9  
Old 11-17-2005, 12:52 PM
jnt4evrb jnt4evrb is offline
Regular
 
Join Date: Oct 2005
Posts: 65
Default

Still not working

Here is what I have...

'Display Totals in labels
mins = DateDiff("m", dteInfonetStart, dteInfonet)
totalTime = Format$(CLng(mins / 60) & ":" & mins Mod 60, "hh:mm")

lblInfonetTotal.Caption = totalTime
Reply With Quote
  #10  
Old 11-17-2005, 01:43 PM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

Simple subtraction is a perfectly valid method of calculating time differences.
This works just fine for me:
Code:
dteInfonetStart = TimeSerial(22, 30, 0) dteInfonet = TimeSerial(22, 35, 0) Debug.Print Format$(dteInfonet - dteInfonetStart, "hh:nn") 'prints 00:05
DateDiff is useful if you want the difference measured in a particular unit. For example if you want the difference in weeks between two dates.
In this case, DateDiff just complicates things because then you need to do all the division and Mod etc.

If, however, you prefer DateDiff for some reason, then use "n" as interval and not "m" ("m" is for months).
Reply With Quote
  #11  
Old 11-18-2005, 07:26 AM
jnt4evrb jnt4evrb is offline
Regular
 
Join Date: Oct 2005
Posts: 65
Default

This code does work however you have put the values in the code and while the start time never changes the dteInfonet in input data that will change everyday it is entered. so I need it to read the input value and minus it from the start value.
Ending time minus Start Time = Total time it took to backup servers. This is very close but not quite right yet. Any other ideas?
Reply With Quote
  #12  
Old 11-18-2005, 12:49 PM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default


The first two lines of code were there just to provide some sample data...

The 3rd line of code is all you need.
Reply With Quote
  #13  
Old 11-18-2005, 02:29 PM
jnt4evrb jnt4evrb is offline
Regular
 
Join Date: Oct 2005
Posts: 65
Default

I am still getting the 00:00 in the results. Can anyone tell me what I am doing wrong.
Reply With Quote
  #14  
Old 11-18-2005, 02:44 PM
LaVolpe's Avatar
LaVolpe LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

What is your line of code now? Probably somethin' really simple gone wrong.
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->