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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Date With AM PM 1

Status
Not open for further replies.

Lambro

Programmer
Aug 22, 2001
258
US
I'm using the now() function to get the date. I need to format it before it gets written to the database.

Examples:
Was: 6/29/2004 12:41:04 PM Needs to be: 062904124104PM

Was: 11/9/2004 1:41:04 PM Needs to be: 110904134104PM

The time needs to be military time.
 
Hi,

A bit of ASP.net script, it should be fairly simple to convert to classic ASP, but i gotta rush, sorry :(

Dim myD As DateTime = Now()
Dim myS As String = myD.ToString
myS = myS.Replace(" ", "")
myS = myS.Replace("/", "")
myS = myS.Replace(":", "")
 
unfortunately that wont add in the padded zeros for like 12/04 when it's reporting as 12/4

uhm .. lemme think on this one a few, i'll post back if someone hasnt beaten me to it.

[thumbsup2]DreX
aKa - Robert
 
Hopefully a quick fix till DreX gets back with summin much better.

Code:
response.write(FormatDate(Now())

Function FormatDate(dDate)

	if not isdate(dDate) then
		dDate = date()
	end if

	strDay = Day(dDate)
	If Len(strDay) = 1 Then strDay = "0" & strDay
	
	strMonth = month(dDate)
	If Len(strMonth) = 1 Then strMonth = "0" & strMonth

	strYear = Year(dDate)
	If Len(strYear) = 4 Then strYear = Mid(strYear,3,2)
	
	strHour = Hour(dDate) Mod 12
	If strHour = 0 Then strHour = 12
	
	If Hour(dDate) < 12 Then
		strAMPM = "AM"
	Else
		strAMPM = "PM"
	End If

	strMinute = Minute(dDate)
	If Len(strMinute) = 1 Then strMinute = "0" & strMinute
	
	strSecond = Second(dDate)
	If Len(strSecond) = 1 Then strSecond = "0" & strSecond
	
	sFinal = strMonth & strDay & strYear & strHour & strMinute & strSecond & strAMPM

	FormatDate = sFinal
End Function

- FateFirst
 
dumb question as i'm working on this ... why AM/PM if it's in military time?

[thumbsup2]DreX
aKa - Robert
 
well the solution i'd have is rather potent, and would make life alot easier, but you'd need access to installing server side components, if not i'll figure something else out, in the meantime if you do, look at this :


and make up the component, if you dont have access to VB i can make the dll for download.

[thumbsup2]DreX
aKa - Robert
 
Code:
<!-- in page -->
<%=customdatetime(now())%>

<!-- Functions -->
<%
Function customdatetime(datetimeval)
  if isdate(datetimeval) then 
    datetimeval = cdate(datetimeval)
    customdatetime = customdatetime & padder(month(datetimeval),2) & padder(day(datetimeval),2) & right(year(datetimeval),2)
    customdatetime = customdatetime & replace(formatdatetime(datetimeval,4),":","")
    customdatetime = customdatetime & mid(formatdatetime(datetimeval,3),instr(datetimeval," ")-1)
  else
    customedatetime = "ERROR-Input not valid datetime value"
  end if
end function
function padder(value,length)
  value = cstr(value)
  length = cint(length)
  for i=len(value) to (length-1)
    value = "0" & value
  next
  padder=value
end function

[thumbsup2]DreX
aKa - Robert
 
Thanks for the help, and no AM and PM are not neccessary for military time.
 
not a problem , hope this gets you patched up

[thumbsup2]DreX
aKa - Robert
 
I need to fix this code to display the date as 2000716 instead of 071604:

Function DateToString(xx)
Dim dd, mm, yy
xx = Request.Form("PaymentDate")
dd = ZeroFill(Day(xx),2)
mm = ZeroFill(Month(xx),2)
yy = ZeroFill(Right(Year(xx),2),2)

DateToString = mm & dd & yy
End Function
 
why pass xx to the function if you're just going to request it anyhow?

nonetheless i dont understand the new format, 2000716? 3 digits from the year?

[thumbsup2]DreX
aKa - Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top