Need help getting two digit month and day values...

  • Thread starter Thread starter Anon
  • Start date Start date
A

Anon

Hello All!

I have written a simple app that auto downloads a file from a secure
ftp, renames it, and moves it to a network location. Everything works
great except the renaming part. I parse out the last write date to get
the new filename, but the days and month are only single digits! This is
a problem when you reach something like 01/11/2004 and 11/1/2004, I get
dup filenames! ie.. 2004111.txt.
any idea on how I can rename these files with 2 digit values?

TIA

Luis
 
Hello All!

I have written a simple app that auto downloads a file from a secure
ftp, renames it, and moves it to a network location. Everything works
great except the renaming part. I parse out the last write date to get
the new filename, but the days and month are only single digits! This is
a problem when you reach something like 01/11/2004 and 11/1/2004, I get
dup filenames! ie.. 2004111.txt.
any idea on how I can rename these files with 2 digit values?

TIA

Luis

Build it with String.Format() like this:

Dim theTime As DateTime = Now 'Or whenever..

Dim szFilename As String = String.Format( _
"{0:D4}{1:D2}{2:D2}.TXT", _
theTime.Year(),
theTime.Month(),
theTime.Day())


// CHRIS
 
Anon,
I would use a format string such as:

Const format As String = "{0:yyyyMMdd}.txt"

Dim theDate As DateTime = DateTime.Now

Dim szFilename As String = String.Format( format, theDate)

Alternatively you could use:

Dim szFilename = theDate.ToString("yyyyMMdd") & ".txt"

For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

Hope this helps
Jay
 
Wow, i used the first response first and it worked great and my next
question was what it all meant. I understand, just not 100%, thanks for
the links and all of the help!!

Luis
 
Anon,
String.Format expects a format specifier and zero or more arguments
(ParamArray).

The format specifier is a string that has zero or more placeholders (one for
each argument). The place holders are in curly braces {}. The number
indicates which argument to use. The info after the : in the curly braces is
used to format the argument. In this case argument zero is a date, so info
specifies a custom date format. The custom date format "yyyyMMdd" says we
want a 4 digit year, a 2 digit month, and a 2 digit day. Basically
String.Format takes the format from the placeholder & calls ToString with
the format, as my second example is doing directly.

The second link (along with its sub topics) should explain in detail how
both work.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top