[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

How can I get the format ddd-mmm-yy hh:mm AMPM from dd/mm/yy hh:mm:ss AMPM?

Hi Experts,

How can I get the format ddd,mmm-dd,yyyy hh:mm AMPM from dd/mm/yy hh:mm:ss AMPM?

I want it to look like this:

Wed, Apr-05,2005 12:00 PM    from    05/04/05 12:00:00 PM

THANKS
0
indyng
Asked:
indyng
  • 4
  • 3
  • 2
2 Solutions
 
kevp75Commented:
i have a great function for formatting dates any which way but loose.  check it out:

'Format Date
'Usage FormatDate("switch(es) to use", DateToFormat)
' "a" = am or pm
' "A" = AM or PM
' "B" = Swatch Internet Time
' "d" = day of month with leading 0
' "D" = day of week, 3 letter
' "F" = long month name
' "g" = hour, 12-hour non-leading 0 format
' "G" = hour, 24-hour non-leading 0 format
' "h" = hour, 12-hour leading 0 format
' "H" = hour, 24-hour leading 0 format
' "i" = minutes
' "j" = day of month, non-leading 0
' "l" = long day name
' "L" = boolean leap year, 0 = no, 1 = yes
' "m" = month number leading 0
' "M" = 3 letter month name
' "n" = month number non-leading 0
' "r" = RFC 822 formatted date/time
' "s" = seconds
' "S" = th, nd, rd, etc...
' "t" = number of days in a given month
' "U" = UNIX timestamp
' "w" = numeric day of week
' "W" = ISO-8601 week number of the year
' "Y" = long year
' "y" = short year
' "z" = day of year
Private Function InternetTime()
      Dim lLngTime
      Dim lLngBeats
      Dim lLngBeatsRound
      lLngTime = (Hour(DateToFormat) * 3600 * 1000) + (Minute(DateToFormat) * 60 * 1000) + (Second(DateToFormat) * 1000 + 3600000)
      lLngBeats = lLngTime / 86400
      lLngBeatsRound = Round(lLngBeats)
      InternetTime = "@" & lLngBeatsRound
      If lLngBeatsRound > 1000 Then InternetTime = "@0" & lLngBeatsRound
      If lLngBeatsRound > 100 Then InternetTime = "@0" & lLngBeatsRound
End Function ' InternetTime()
Private Function LeadingZero(ByRef pStrValue)
      If Len(pStrValue) < 2 Then pStrValue = "0" & pStrValue
      LeadingZero = pStrValue
End Function ' LeadingZero(ByRef pStrValue)
Private Function IsLeap()
      Dim lLngYear
      lLngYear = Year(DateToFormat)
      IsLeap = 0
      If (lLngYear Mod 4 = 0) And (lLngYear Mod 100 <> 0) Or (lLngYear Mod 400 = 0) Then IsLeap = 1
End Function ' IsLeap()
Private Function DaysInMonth()
      Dim lLngMonth
      lLngMonth = Month(DateToFormat)
      Select Case lLngMonth
            Case 9, 4, 6, 11 : DaysInMonth = 30
            Case 2 : If CBool(IsLeap()) Then DaysInMonth = 29 Else DaysInMonth = 28
            Case Else : DaysInMonth = 31
      End Select
End Function ' DaysInMonth()
Private Function FormatHour()
      Dim lDtmNow
      lDtmNow = FormatDateTime(DateToFormat,3)
      FormatHour = Left(lDtmNow,InStr(lDtmNow,":") - 1)
End Function ' FormatHour()
Private Function OrdinalSuffix()
      Dim lLngDay
      lLngDay = Day(DateToFormat)
      OrdinalSuffix = "th"
      If lLngDay = 1 Then OrdinalSuffix = "st"
      If lLngDay = 2 Then OrdinalSuffix = "nd"
      If lLngDay = 3 Then OrdinalSuffix = "rd"
End Function ' OrdinalSuffix()
Public Function FormatDate(ByRef pStrDate, DateToFormat)
      ' Define local variables
      Dim lObjRegExp
      Dim lObjRegMatches
      Dim lObjMatch
      Dim lDtmNow
      Dim lLngSecond
      Dim lLngMinute
      Dim lLngHour
      Dim lLngDay
      Dim lLngWeekDay
      Dim lLngMonth
      Dim lLngYear
      lDtmNow = DateToFormat
      lLngSecond = Second(lDtmNow)
      lLngMinute = Minute(lDtmNow)
      lLngHour = Hour(lDtmNow)
      lLngDay = Day(lDtmNow)
      lLngWeekDay = WeekDay(lDtmNow)
      lLngMonth = Month(lDtmNow)
      lLngYear = Year(lDtmNow)
      ' Prepare RegExp object and set parameters
      Set lObjRegExp = New RegExp
      lObjRegExp.Pattern = "([a-z][a-z]*[a-z])*[a-z]"
      lObjRegExp.Global = True
      lObjRegExp.IgnoreCase = True
      Set lObjRegMatches = lObjRegExp.Execute(pStrDate)
      ' List each individual match and compare to different date functoids
      For Each lObjMatch In lObjRegMatches
            Select Case lObjMatch.Value
                  Case "a" : pStrDate = Replace(pStrDate,"a",LCase(Right(lDtmNow,2)))
                  Case "A" : pStrDate = Replace(pStrDate,"A",UCase(Right(lDtmNow,2)))
                  Case "B" : pStrDate = Replace(pStrDate,"B",InternetTime())
                  Case "d" : pStrDate = Replace(pStrDate,"d",LeadingZero(lLngDay))
                  Case "D" : pStrDate = Replace(pStrDate,"D",Left(WeekDayName(lLngWeekDay),3))
                  Case "F" : pStrDate = Replace(pStrDate,"F",MonthName(lLngMonth))
                  Case "g" : pStrDate = Replace(pStrDate,"g",FormatHour())
                  Case "G" : pStrDate = Replace(pStrDate,"G",lLngHour)
                  Case "h" : pStrDate = Replace(pStrDate,"h",LeadingZero(FormatHour()))
                  Case "H" : pStrDate = Replace(pStrDate,"H",LeadingZero(lLngHour))
                  Case "i" : pStrDate = Replace(pStrDate,"i",LeadingZero(lLngMinute))
                  Case "j" : pStrDate = Replace(pStrDate,"j",lLngDay)
                  Case "l" : pStrDate = Replace(pStrDate,"l",WeekDayName(lLngWeekDay))
                  Case "L" : pStrDate = Replace(pStrDate,"L",IsLeap())
                  Case "m" : pStrDate = Replace(pStrDate,"m",LeadingZero(lLngMonth))
                  Case "M" : pStrDate = Replace(pStrDate,"M",Left(MonthName(lLngMonth),3))
                  Case "n" : pStrDate = Replace(pStrDate,"n",lLngMonth)
                  Case "r" : pStrDate = Replace(pStrDate,"r",Left(WeekDayName(lLngWeekDay),3) & ", " & _
                        lLngDay & " " & Left(MonthName(lLngMonth),3) & " " & lLngYear & " " & FormatDateTime(DateToFormat,4))
                  Case "s" : pStrDate = Replace(pStrDate,"s",LeadingZero(lLngSecond))
                  Case "S" : pStrDate = Replace(pStrDate,"S",OrdinalSuffix())
                  Case "t" : pStrDate = Replace(pStrDate,"t",DaysInMonth())
                  Case "U" : pStrDate = Replace(pStrDate,"U",DateDiff("s",DateSerial(1970, 1, 1),lDtmNow))
                  Case "w" : pStrDate = Replace(pStrDate,"w",lLngWeekDay - 1)
                  Case "W" : pStrDate = Replace(pStrDate,"W",DatePart("ww", Date(), 2, vbFirstFourDays))
                  Case "Y" : pStrDate = Replace(pStrDate,"Y",lLngYear)
                  Case "y" : pStrDate = Replace(pStrDate,"y",Right(lLngYear,2))
                  Case "z" : pStrDate = Replace(pStrDate,"z",DatePart("d", Date(), 2))
                  Case Else : pStrDate = pStrDate & ""
            End Select
      Next
      Set lObjRegExp = Nothing
      FormatDate = pStrDate
End Function ' FormatDate(ByRef pStrDate)
0
 
CirienoCommented:
In the same vein, this is one I wrote a number of years ago -- you've just given me the boost to give it a polish, thanks :)
The advantage of this one is that the switches are case-sensitive, so you could have other words in the string and parse a whole string at once.
Another addition is the ability to have a time offset for working with those tricky timezones and daylight savings issues.

    tempDate = cdate("2006-4-5 12:00:00")     '/* your date as above */
    response.write( newformatDateTime(tempDate, "W2, M4-D2,Y2 H3:N2 F2", 0) )
    response.write( newformatDateTime(now, "<br />Today is W3 the D3 of M5 in the year Y2", 0) )


function newformatDateTime(dtmInput, strFormat, intOffset)
    '/* D1 = 1 : D2 = 01 : D3 = 1st */
    '/* W1 = M : W2 = Mon : W3 = Monday */
    '/* M1 = 1 : M2 = 01 : M3 = J : M4 = Jan : M5 = January */
    '/* Y1 = 06 : Y2 = 2006 */
    '/* H1 = 12-hr clock : H2 = 12-hr clock with zero : H3 = 24-hr clock : H4 = 24-hr clock with zero */
    '/* N1 = minutes : N2 = minutes with zero */
    '/* S1 = seconds : S2 = seconds with zero */
    '/* F1 = am/pm : F2 = AM/PM */
    '/* ISO = Y2-M1-D1 H1:N1:S1 */

    dim intDay, intMonth, intYear, intHour, intMinute, intSecond, intDayOfWeek

    if isNull(dtmInput) then
        exit function
    end if
    if (not isDate(dtmInput)) then     '/* fail - not a real date */
        exit function
      end if
    if (not isNumeric(intOffset)) then     '/* fail - not a useful time offset */
        exit function
    end if

    '/* set the timezone difference if needed, this also works with negative offset */
    dtmInput = dateadd("h", intOffset, dtmInput)

    intDay = datepart("d", dtmInput)
    intMonth = datepart("m", dtmInput)
    intYear = datepart("yyyy", dtmInput)
    intHour = datepart("h", dtmInput)
    intMinute = datepart("n", dtmInput)
    intSecond = datepart("s", dtmInput)
    intDayOfWeek = weekday(dtmInput)

    if (strFormat = "ISO") then     '/* useful for entering into databases */
        newformatDateTime = intYear & "-" & leadingZero(intMonth) & "-" & leadingZero(intDay) & " "_
            & leadingZero(intHour) & ":" & leadingZero(intMinute) & ":" & leadingZero(intSecond)
        exit function
    end if
   
    strFormat = replace(strFormat, "D1", intDay, 1, -1, 0)
    strFormat = replace(strFormat, "D2", leadingZero(intDay), 1, -1, 0)
    if (intDay=1) or (intDay=21) or (intDay=31) then
        strFormat = replace(strFormat, "D3", intDay & "st", 1, -1, 0)
    elseif (intDay=2) or (intDay=22) then
        strFormat = replace(strFormat, "D3", intDay & "nd", 1, -1, 0)
    elseif (intDay=3) or (intDay=23) then
        strFormat = replace(strFormat, "D3", intDay & "rd", 1, -1, 0)
    else
        strFormat = replace(strFormat, "D3", intDay & "th", 1, -1, 0)
    end if

    strFormat = replace(strFormat, "W1", left(weekdayname(intDayOfWeek),1), 1, -1, 0)
    strFormat = replace(strFormat, "W2", weekdayname(intDayOfWeek,1), 1, -1, 0)
    strFormat = replace(strFormat, "W3", weekdayname(intDayOfWeek,0), 1, -1, 0)

    strFormat = replace(strFormat, "M1", intMonth, 1, -1, 0)
    strFormat = replace(strFormat, "M2", leadingZero(intMonth), 1, -1, 0)
    strFormat = replace(strFormat, "M3", left(monthname(intMonth),1), 1, -1, 0)
    strFormat = replace(strFormat, "M4", monthname(intMonth,1), 1, -1, 0)
    strFormat = replace(strFormat, "M5", monthname(intMonth), 1, -1, 0)

    strFormat = replace(strFormat, "Y1", right(intYear,2), 1, -1, 0)
    strFormat = replace(strFormat, "Y2", intYear, 1, -1, 0)

    if (intHour < 12) then
        strFormat = replace(strFormat, "H1", intHour, 1, -1, 0)
        strFormat = replace(strFormat, "H2", leadingZero(intHour), 1, -1, 0)
        strFormat = replace(strFormat, "F1", "am", 1, -1, 0)
        strFormat = replace(strFormat, "F2", "AM", 1, -1, 0)
    else
        strFormat = replace(strFormat, "H1", (intHour-12), 1, -1, 0)
        strFormat = replace(strFormat, "H2", leadingZero(intHour-12), 1, -1, 0)
        strFormat = replace(strFormat, "F1", "pm", 1, -1, 0)
        strFormat = replace(strFormat, "F2", "PM", 1, -1, 0)
    end if
    strFormat = replace(strFormat, "H3", intHour, 1, -1, 0)
    strFormat = replace(strFormat, "H4", leadingZero(intHour), 1, -1, 0)

    strFormat = replace(strFormat, "N1", intMinute, 1, -1, 0)
    strFormat = replace(strFormat, "N2", leadingZero(intMinute), 1, -1, 0)

    strFormat = replace(strFormat, "S1", intSecond, 1, -1, 0)
    strFormat = replace(strFormat, "S2", leadingZero(intSecond), 1, -1, 0)

    newformatDateTime = strFormat
end function




function leadingZero(intInput)
    dim strNumber

    if not isNumeric(intInput) then
        exit function
    end if    

    if (intInput>=0) and (intInput<=9) then
        strNumber = "0" & intInput
    else
        strNumber = intInput
    end if

    leadingZero = strNumber
end function
0
 
indyngAuthor Commented:
Is there anything more simple?

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
indyngAuthor Commented:
I have

Response.Write WeekdayName(DatePart("W", Now),1) & ", " & MonthName(Month(Now), True) & "-" & Day(Now()) & ", " & Year(Now) & " "  & "<br>"

But I just need the time in hh:nn AMPM.

Thanks
0
 
kevp75Commented:
you've got the simplest way to do it.  I'm sure there is a way, maybe using Mid to get rid of the seconds.  Possibly even using regExp.

I'll see if I can come up with something along those lines
0
 
CirienoCommented:
The examples are my function:

    datepart("h", now) & ":" & datepart("n", now)

but you should use the leadingZero function to make sure the time doesn't look like 3:5 for five minutes past three in the morning.

    leadingZero(datepart("h", now)) & ":" & leadingZero(datepart("n", now))

To be honest, it doesn't get much more simple than being given a whole function to cut and paste into your script and the usage examples to use it!..
0
 
kevp75Commented:
i semi-agree with Cirieno.

to use mine like you want it:
FormatDate("D, M-d,Y g:i A", NOW())

however I'm looking around for something a bit "easier" by using regExp
0
 
indyngAuthor Commented:
kevp75 ,

I tried your script and got this error:

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'lDtmNow'
/intermodal/shipment_tracker/functions/MyDateFormat.txt, line 90

line 90 is:

lLngSecond = Second(lDtmNow)

Thanks
0
 
kevp75Commented:
odd.
FormatDate("D, M-d,Y g:i A", NOW()) for me puts out Fr13, PMpr-07,2006 12:13 PM

see:http://www.portalfanatic.com/test14.asp
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now