//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Date/Time Routines :: How do I display time in military format?


How do I display time in military format?

There are at least three options for this. 
 
One is to change regional settings to display time in military format. I don't like to do this because it can break existing code, and can change depending on who is logged into the server (if anyone). 
 
The second is to use string formatting. Here is an example: 
 
<% 
    ft = formatdatetime(time(),3) 
    response.write "Standard time:<p>" & formatdatetime(ft,3) 
    if right(ft,2)="PM" then  
        t = split(ft,":")  
        milhour = clng(t(0)) 
        if clng(left(ft,2))<12 then milhour = milhour +12  
        mtime = cstr(milhour) & ":" & t(1)  
        mtime = mtime & ":" & left(t(2),2)  
    elseif clng(left(ft,2))=12 then ' this handles midnight only 
        mtime = "00:" 
        mins = datepart("n",ft) 
        secs = datepart("s",ft) 
        mtime = mtime & left("00",2-len(mins)) & mins 
        mtime = mtime & left("00",2-len(secs)) & ":" & secs 
    else 
        mtime = left(ft,len(ft)-3)  
    end if  
    response.write "<p>Military time:<p>" & mTime  
%>
 
The third is to use SQL Sevrer to convert it for you: 
 
<% 
    response.write "Standard time:<p>" & time() 
    set conn = CreateObject("ADODB.Connection") 
    conn.open "<connection string>" 
    sql = "SELECT CONVERT(CHAR(8),(CONVERT(DATETIME,CURRENT_TIMESTAMP,113)),114)" 
    set rs = conn.execute(sql) 
    response.write "<p>Military time:<p>" & rs(0) 
    rs.close: set rs = nothing 
%>
 
The advantage with SQL is you could change it to CHAR(12) and get millisecond accuracy (the above scripts only get down to the second).

Related Articles

Can I get millisecond accuracy in ASP?
Can I make VBScript format dates for me?
Could I get a little help with dates?
Given a date, how do I find the beginning and end of that week?
Given two dates, how do I determine an age?
How do I calculate dates, such as the first day of the month?
How do I convert a DATEDIFF to days, hours, and minutes?
How do I convert a timespan, in seconds, to HH:MM:SS?
How do I convert local time to UTC (GMT) time?
How do I count the number of business days between two dates?
How do I delimit/format dates for database entry?
How do I determine the number of seconds since 1/1/1970?
How do I implement a calendar / datepicker in ASP?
How do I select time only from a DATETIME column?
Why do I have problems inserting NOW() into a database?
Why does JavaScript's document.lastModified() not work in ASP files?

 

 


Created: 5/31/2001 | Last Updated: 3/24/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (360)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...