Solved

Show Current Date

Posted on 2006-11-14
9
645 Views
Last Modified: 2012-06-21
How to show Current date as "DD/MM/YYYY" in vbscript
0
Comment
Question by:dion_p1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17937089
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17937091
does this work?

strDateTime = formatDate("%m/$d/%Y", UDate(Now()))

http://www.ilovejackdaniels.com/asp/vbscript-date-format-functions/
0
 

Author Comment

by:dion_p1
ID: 17937119
Nope i get UDATE Error.

i can return date and time by typing in

strCDate = date()
msgbox strCDate

but i dont wanth the time there and i want to change the "/" to "-"
0
Independent Software Vendors: 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!

 
LVL 35

Expert Comment

by:Raynard7
ID: 17937252
replace(FormatDateTime(Date(),2), "/", "-")
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17937550
>Nope i get UDATE Error.
??? You asked to "display" a date value, and now you speak about an UPDATE error?
can you show us how this can be related?
0
 

Author Comment

by:dion_p1
ID: 17937651
I get cannot use parentheses when calling a sub

replace(FormatDateTime(Date(),2), "/", "-")
msgbox strDateTime
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17938793
This will put the parts in the right order regardless of the system settings:

    strDate = DatePart("d", Date) & "-" & DatePart("m", Date) & "-" & DatePart("yyyy", Date)
    MsgBox strDate
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 17938842

'Usage for "DD/MM/YYYY":
fncFmtDateFast(Date,"%D-%M-%Y")


Function fncFmtDateFast( _
    ByVal strDate, _
    ByRef strFormat _
   )
 ' Accepts strDate as a valid date/time,
 ' strFormat as the output template.
 ' The function finds each item in the
 ' template and replaces it with the
 ' relevant information extracted from strDate

 ' Template items (example)
 ' %m Month as a decimal (02)
 ' %M Month as a decimal (02)
 ' %b Abbreviated month name (Feb )
 ' %D Day of the month (23)
 ' %Y Year with century (1998)
 ' %H Hour in 24 hour format (24)
 ' %N Minute as an integer (01)
 ' %S Second as an integer (55)

 On Error Resume Next

 Dim str24HourPart
 Dim strMinutePart
 Dim strSecondPart

 ' Insert Abbreviated Month Names
 strFormat = Replace(strFormat, "%b", _
          MonthName(DatePart("m", strDate), _
          True), 1, -1, vbBinaryCompare)
         
 ' Insert Day Of Month
 strFormat = Replace(strFormat, "%D", _
          Right("0" & DatePart("d", strDate), 2), 1, _
          -1, vbBinaryCompare)

 ' Insert Long Year (4 digit)
 strFormat = Replace(strFormat, "%Y", _
          DatePart("yyyy", strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Hour in 24hr format
 str24HourPart = DatePart("h", strDate)
 If Len(str24HourPart) < 2 Then str24HourPart = "0" & _
                                                 str24HourPart
 strFormat = Replace(strFormat, "%H", str24HourPart, 1, _
          -1, vbBinaryCompare)

 ' Insert Minutes
 strMinutePart = DatePart("n", strDate)
 If Len(strMinutePart) < 2 Then _
          strMinutePart = "0" & strMinutePart
 strFormat = Replace(strFormat, "%N", strMinutePart, _
          1, -1, vbBinaryCompare)

 ' Insert Seconds
 strSecondPart = DatePart("s", strDate)
 If Len(strSecondPart) < 2 Then _
          strSecondPart = "0" & strSecondPart
 strFormat = Replace(strFormat, "%S", strSecondPart, 1, _
          -1, vbBinaryCompare)

 fncFmtDateFast = strFormat

 'If there is an error output its value
 If Err.Number <> 0 Then
  Response.Clear
  Response.Write "ERROR " & Err.Number & _
                 ": fmcFmtDate - " & Err.Description
  Response.Flush
  Response.End
 End If
End Function ' fncFmtDateFast


Function fncGetDayOrdinal( _
 byVal intDay _
 )
 ' Accepts a day of the month as an integer and returns the
 ' appropriate suffix

 Dim strOrd

 Select Case intDay
 Case 1, 21, 31
  strOrd = "st"
 Case 2, 22
  strOrd = "nd"
 Case 3, 23
  strOrd = "rd"
 Case Else
  strOrd = "th"
 End Select

 fncGetDayOrdinal = strOrd
End Function ' fncGetDayOrdinal



Function fncFmtDate( _
    byVal strDate, _
    byRef strFormat _
   )
 ' Accepts strDate as a valid date/time,
 ' strFormat as the output template.
 ' The function finds each item in the
 ' template and replaces it with the
 ' relevant information extracted from strDate

 ' Template items (example)
 ' %m Month as a decimal (02)
 ' %B Full month name (February)
 ' %b Abbreviated month name (Feb )
 ' %d Day of the month (23)
 ' %D Day of the month (23)
 ' %O Ordinal of day of month (eg st or rd or nd)
 ' %j Day of the year (54)
 ' %Y Year with century (1998)
 ' %y Year without century (98)
 ' %w Weekday as integer (0 is Sunday)
 ' %a Abbreviated day name (Fri)
 ' %A Weekday Name (Friday)
 ' %H Hour in 24 hour format (24)
 ' %h Hour in 12 hour format (12)
 ' %N Minute as an integer (01)
 ' %n Minute as optional if minute <> 0
 ' %S Second as an integer (55)
 ' %P AM/PM Indicator (PM)

 On Error Resume Next

 Dim intPosItem
 Dim int12HourPart
 Dim str24HourPart
 Dim strMinutePart
 Dim strSecondPart
 Dim strAMPM

 ' Insert Month Numbers
 strFormat = Replace(strFormat, "%m", _
          DatePart("m", strDate), 1, -1, vbBinaryCompare)

' Insert Month Numbers
 strFormat = Replace(strFormat, "%M", _
          Right("0", DatePart("m", strDate), 2), 1, -1, vbBinaryCompare)

 ' Insert non-Abbreviated Month Names
 strFormat = Replace(strFormat, "%B", _
          MonthName(DatePart("m", strDate), _
          False), 1, -1, vbBinaryCompare)

 ' Insert Abbreviated Month Names
 strFormat = Replace(strFormat, "%b", _
          MonthName(DatePart("m", strDate), _
          True), 1, -1, vbBinaryCompare)

 ' Insert Day Of Month
 strFormat = Replace(strFormat, "%d", _
          DatePart("d",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Day Of Month
 strFormat = Replace(strFormat, "%D", _
          Right("0" & DatePart("d", strDate), 2), 1, _
          -1, vbBinaryCompare)

 ' Insert Day of Month Ordinal (eg st, th, or rd)
 strFormat = Replace(strFormat, "%O", _
          fncGetDayOrdinal(Day(strDate)), _
          1, -1, vbBinaryCompare)

 ' Insert Day of Year
 strFormat = Replace(strFormat, "%j", _
          DatePart("y",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Long Year (4 digit)
 strFormat = Replace(strFormat, "%Y", _
          DatePart("yyyy",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Short Year (2 digit)
 strFormat = Replace(strFormat, "%y", _
          Right(DatePart("yyyy",strDate),2), _
          1, -1, vbBinaryCompare)

 ' Insert Weekday as Integer (eg 0 = Sunday)
 strFormat = Replace(strFormat, "%w", _
          DatePart("w",strDate,1), 1, _
          -1, vbBinaryCompare)

 ' Insert Abbreviated Weekday Name (eg Sun)
 strFormat = Replace(strFormat, "%a", _
          WeekDayName(DatePart("w",strDate,1),True), 1, _
          -1, vbBinaryCompare)

 ' Insert non-Abbreviated Weekday Name
 strFormat = Replace(strFormat, "%A", _
          WeekDayName(DatePart("w",strDate,1),False), 1, _
          -1, vbBinaryCompare)

 ' Insert Hour in 24hr format
 str24HourPart = DatePart("h",strDate)
 If Len(str24HourPart) < 2 then str24HourPart = "0" & _
                                                 str24HourPart
 strFormat = Replace(strFormat, "%H", str24HourPart, 1, _
          -1, vbBinaryCompare)

 ' Insert Hour in 12hr format
 int12HourPart = DatePart("h",strDate) Mod 12
 If int12HourPart = 0 then int12HourPart = 12
 strFormat = Replace(strFormat, "%h", int12HourPart, 1, _
          -1, vbBinaryCompare)

 ' Insert Minutes
 strMinutePart = DatePart("n",strDate)
 If Len(strMinutePart) < 2 then _
          strMinutePart = "0" & strMinutePart
 strFormat = Replace(strFormat, "%N", strMinutePart, _
          1, -1, vbBinaryCompare)

 ' Insert Optional Minutes
 If CInt(strMinutePart) = 0 then
  strFormat = Replace(strFormat, "%n", "", 1, _
           -1, vbBinaryCompare)
 Else
  If CInt(strMinutePart) < 10 then _
           strMinutePart = "0" & strMinutePart
  strMinutePart = ":" & strMinutePart
  strFormat = Replace(strFormat, "%n", strMinutePart, _
           1, -1, vbBinaryCompare)
 End if

 ' Insert Seconds
 strSecondPart = DatePart("s",strDate)
 If Len(strSecondPart) < 2 then _
          strSecondPart = "0" & strSecondPart
 strFormat = Replace(strFormat, "%S", strSecondPart, 1, _
          -1, vbBinaryCompare)

 ' Insert AM/PM indicator
 If DatePart("h",strDate) >= 12 then
   strAMPM = "PM"
 Else
   strAMPM = "AM"
 End If

 strFormat = Replace(strFormat, "%P", strAMPM, 1, _
          -1, vbBinaryCompare)

 fncFmtDate = strFormat

 'If there is an error output its value
 If err.Number <> 0 then
  Response.Clear
  Response.Write "ERROR " & err.Number & _
                 ": fmcFmtDate - " & err.Description
  Response.Flush
  Response.End
 End if
End Function ' fncFmtDate
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 500 total points
ID: 17941277
here's a method to be able to use vb's format function in vbs

Function Format(vExpression, sFormat)
         set fmt = CreateObject("MSSTDFMT.StdDataFormat")
        fmt.Format = sFormat
 
        set rs = CreateObject("ADODB.Recordset")
        rs.Fields.Append "fldExpression", 12 ' adVariant
 
        rs.Open
        rs.AddNew
 
        set rs("fldExpression").DataFormat = fmt
        rs("fldExpression").Value = vExpression
 
        Format = rs("fldExpression").Value
 
        rs.close: Set rs = Nothing: Set fmt = Nothing
 
End Function

msgbox format(now(),"dd-mm-yyyy")
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBS file using code from 2nd file (txt or vbs) 4 47
Adding to a VBA? 6 84
How to make an ADE file by code? 11 99
VB script to continue despite error 2 49
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question