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!

Date format for military time

Status
Not open for further replies.

boettger

Programmer
Sep 27, 2000
15
US
Hi:
This is the code that I have in my ASP page.
value=<%= formatdatetime(now,vbshorttime) %> name="timestarted" >
This displays military time as 13:12 and my customer puts in a start and end time and I get the number of minutesfor the task. My customer wants the format of military time to be without the colon (ex. 1312). I've tried changing it to a number but it has to be handled as a date since I'm use DateDiff. I've also tried to use the DatePart command but there must be a simple way to do it.
Can anyone help???
Thanks
 
Create a string by replacing the : with "".

strDisplay = Replace(dteTime, ":", "")

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
As long as the value is properly 0-filled (which it should be if it is in military or 24-hour format) then breakingit back into it's components is fairly easy. Example:
Code:
<%
'Function to convert a Date/Time object to a 24-hour string
Function CTimeTo24Str(aTime)
   If isDate(aTime) Then
      CTimeTo24Str = ZeroFill(Hour(aTime),2) & ZeroFill(Minute(aTime),2)
   Else
      err.Raise 12345,"Conversion to 24-Hour Time","Date Conversion from a Date/Time to a 24-hour date failed, input date is not a valid date."
   End If
End Function

'Function to convert 24-hour time in a string to a std Date/Time object
Function C24HourToTime(aTimeString)
   If len(aTimeString) = 4 And isNumeric(aTimeString) Then
      C24HourToTime = TimeSerial(Left(aTimeString,2),Right(aTimeString,2),0)
   Else
      err.Raise 12346,"24-Hour Time Conversion", " Conversion from 24-hour time to standard date/Time object failed. Input value is incorrect length or contains non-numeric characters."
   End If
End Function

'Function to zero-fill a string
Function ZeroFill(origStr,targetNumChars)
   If Len(origStr) >= targetNumChars Then
      ZeroFill = origStr
   Else
      ZeroFill = String(targetNumChars - Len(origStr),"0") & origStr
   End If
End Function

'---- Sample using above functions

Dim myTime
myTime = Now()
Response.Write "Original Time: " & FormatDateTime(MyTime,4) & "<br>"

myTime = CTimeTo24Str(myTime)
Response.Write "Converted to 24-hour: " & myTime & "<br>"

myTime = C24HourToTime(myTime)
Response.Write "Converted from 24-hour to DateTime: " & myTime & "<br>"
%>

I added in some error checking code for you as well. You could easily replace my concatenation in the first function with the code provided by TomThumb, provided you specify your dteTime string that he uses in his example to be the output of a FormatDateTime(whatever,4) so that it will be in zerofilled 24-hour format for you already.

-T

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top