?
Solved

How can i Change Date and Time Format VBS

Posted on 2006-11-13
7
Medium Priority
?
2,148 Views
Last Modified: 2007-11-27
I need to change the format of this

From
9/12/2004 12:25
To
9/12/2004

its actually the last value in the csv file...

So it needs to be something like (strData & "," & strNewDateValue & "," & intPassword)

My script needs to write the data back to another file


strInputData = Split(oInputFile.ReadAll, vbNewline)
On Error Resume Next

For each strData In strInputData
Dim intPassword
Randomize
intPassword = Int(1234567 * Rnd() + 1)
      Set file = objFileSys.CreateTextFile(strExportFile,TRUE)
      file.writeline(strData & "," & intPassword)
Next
file.close
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
7 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 17935596
The date will always be stored as dd/mm/yyyy hh:mm:ss in the database, but you can always just display the date part using VB code as Format(YourDateValue, "Short Date")

Hope this helps.
-Jim
0
 
LVL 12

Expert Comment

by:jkaios
ID: 17935648
Or you can also use this, which is independent of the "Short Date" setting in Control Panel:

strNewDateValue = Format(theOriginalDateValue, "MM/dd/yyyy")
0
 
LVL 28

Expert Comment

by:Ark
ID: 17936957
Use Date function instead of Now
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!

 

Author Comment

by:dion_p1
ID: 17937132
How can i replace the "/" to "-"

i have

strCDate = Date()
msgbox strCDate

0
 
LVL 3

Expert Comment

by:ia2189
ID: 17938129
strCDate = Replace(Date, "/", "-")
msgbox strCDate
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 17941039
here's the syntax

format(originaldatevalue,"MM-dd-yyyy")


you can use any combination you want, the MM part will be replaced by the month, the dd with the days, and the yyyy with the year!

0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 2000 total points
ID: 17941189
sorry, i didn't notice the vbs part of the question.
ofcourse the format function works only in vb6.

SOLUTIONS:

1- to be able to format date/time in vbs u have to use the formatdatetime which formats the date in 5 fixed formats: general date, long date, short date, long time, short time.
syntax:
FormatDateTime("6/26/1943", p)

p =0 'vbgeneraldate
p=1 'vblongdate
p=2 'vbshortdate
p=3 'vblongtime
p=4 'vbshorttime


2. however, here's a workaround to be able to use the vb's built in format function.

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")


3. Finally, u can always write your own custom function

dim d
d=now()
msgbox day(d) & "-" & month(d)  & "-" & year(d)


hope this helps,

cheers

dan
0

Featured Post

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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 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