Format a date with dashes? 
Author Message
 Format a date with dashes?

I want to use the current date as part of a file name. Of course, it
can't have slashes (/) in it. But I can't figure out how to format the
date so it uses dashes (-) instead.

How do I get 8-24-2009 instead of 8/24/2009 ?

Thanks.



Sat, 11 Feb 2012 00:05:05 GMT  
 Format a date with dashes?


Quote:
>I want to use the current date as part of a file name. Of course, it
>can't have slashes (/) in it. But I can't figure out how to format the
>date so it uses dashes (-) instead.

>How do I get 8-24-2009 instead of 8/24/2009 ?

>Thanks.

OK - never mind - got it.

I was trying to *Format* the date. Now I'm just using the Replace
function to change the / to -.



Sat, 11 Feb 2012 00:22:52 GMT  
 Format a date with dashes?

Quote:
> I want to use the current date as part of a file name. Of course, it
> can't have slashes (/) in it. But I can't figure out how to format the
> date so it uses dashes (-) instead.

> How do I get 8-24-2009 instead of 8/24/2009 ?

> Thanks.

duh?

Sub asdf()
Dim slask As String
Dim i As Integer

slask = "2009/08/24"

For i = 1 To Len(slask)
  If Mid$(slask, i, 1) = "/" Then Mid$(slask, i, 1) = "-"
Next

MsgBox slask

End Sub

or try something more complicated...

//CY



Sat, 11 Feb 2012 00:23:23 GMT  
 Format a date with dashes?


Quote:

> > I want to use the current date as part of a file name. Of course, it
> > can't have slashes (/) in it. But I can't figure out how to format the
> > date so it uses dashes (-) instead.

> > How do I get 8-24-2009 instead of 8/24/2009 ?

> > Thanks.

> duh?

> Sub asdf()
> Dim slask As String
> Dim i As Integer

> slask = "2009/08/24"

> For i = 1 To Len(slask)
>   If Mid$(slask, i, 1) = "/" Then Mid$(slask, i, 1) = "-"
> Next

> MsgBox slask

> End Sub

> or try something more complicated...

Can you get more complicated?.
Use the Replace command,

  NewDate=Replace(OldDate, "/" ,"-")



Sat, 11 Feb 2012 00:26:02 GMT  
 Format a date with dashes?


Quote:
>I want to use the current date as part of a file name. Of course, it
> can't have slashes (/) in it. But I can't figure out how to format the
> date so it uses dashes (-) instead.

> How do I get 8-24-2009 instead of 8/24/2009 ?

> Thanks.

You pretty much answered your own question.  Use the Format function...

Format$(Date, "yyyy-mm-dd")

or

Format$(Date, "mm-dd-yyyy")

--
Mike



Sat, 11 Feb 2012 00:26:07 GMT  
 Format a date with dashes?


Quote:


>>I want to use the current date as part of a file name. Of course, it
>>can't have slashes (/) in it. But I can't figure out how to format the
>>date so it uses dashes (-) instead.

>>How do I get 8-24-2009 instead of 8/24/2009 ?

>>Thanks.

> OK - never mind - got it.

> I was trying to *Format* the date. Now I'm just using the Replace
> function to change the / to -.

Format is what you SHOULD be doing. If you use Replace, you're going to run
into problems if you get a date that is not using slashes. For example,
20090824 or 08.24.2009.  These are both valid date expressions.  Depending
on the user's date format settings in Control Panel, either of these formats
(and many others) are possible.

Use the Format function as I showed earlier.

--
Mike



Sat, 11 Feb 2012 00:33:29 GMT  
 Format a date with dashes?
replace was nice, never seen it...

//CY



Sat, 11 Feb 2012 01:24:08 GMT  
 Format a date with dashes?

Quote:
> replace was nice, never seen it...

That's because while it solves the immediate problem, it opens a can of
worms if the date separator is not a slash (/). The correct solution is to
use
the format function as indicated by MikeD. Saga


Sat, 11 Feb 2012 05:42:45 GMT  
 Format a date with dashes?


Quote:



>>I want to use the current date as part of a file name. Of course, it
>> can't have slashes (/) in it. But I can't figure out how to format the
>> date so it uses dashes (-) instead.

>> How do I get 8-24-2009 instead of 8/24/2009 ?

>> Thanks.

>You pretty much answered your own question.  Use the Format function...

>Format$(Date, "yyyy-mm-dd")

>or

>Format$(Date, "mm-dd-yyyy")

OK, Mike. I went back and changed to use the Format$ function. That's
what I was trying to do in the first place but I didn't realize that I
could specify the dashes I thought all I could do was the "mm", "dd"
and "yyyy".

Thanks.



Sat, 11 Feb 2012 09:26:37 GMT  
 Format a date with dashes?
You can even include text (it has to be quoted) within the pattern section
of the Format function. Try executing this example statement...

MsgBox Format(Date, """Month: ""m"", Day: ""d"", Year: ""yyyy")

--
Rick (MVP - Excel)


Quote:




>>>I want to use the current date as part of a file name. Of course, it
>>> can't have slashes (/) in it. But I can't figure out how to format the
>>> date so it uses dashes (-) instead.

>>> How do I get 8-24-2009 instead of 8/24/2009 ?

>>> Thanks.

>>You pretty much answered your own question.  Use the Format function...

>>Format$(Date, "yyyy-mm-dd")

>>or

>>Format$(Date, "mm-dd-yyyy")

> OK, Mike. I went back and changed to use the Format$ function. That's
> what I was trying to do in the first place but I didn't realize that I
> could specify the dashes I thought all I could do was the "mm", "dd"
> and "yyyy".

> Thanks.



Sat, 11 Feb 2012 10:24:11 GMT  
 Format a date with dashes?
Just to clarify... you can do this without internally quoting the text part,
but you must "escape" the meta-characters (letters used in patterns) with a
backslash so the Format function doesn't interpret them as patterns (for
example, M for month). Here is the same output as before, but without using
internally quoted text...

MsgBox Format(Date, "\Mo\nt\h: m, \Da\y: d, \Year: yyyy")

Personally, I think it is easier to quote the text then look for each
meta-character in order to "escape" it.

--
Rick (MVP - Excel)


Quote:
> You can even include text (it has to be quoted) within the pattern section
> of the Format function. Try executing this example statement...

> MsgBox Format(Date, """Month: ""m"", Day: ""d"", Year: ""yyyy")

> --
> Rick (MVP - Excel)







>>>>I want to use the current date as part of a file name. Of course, it
>>>> can't have slashes (/) in it. But I can't figure out how to format the
>>>> date so it uses dashes (-) instead.

>>>> How do I get 8-24-2009 instead of 8/24/2009 ?

>>>> Thanks.

>>>You pretty much answered your own question.  Use the Format function...

>>>Format$(Date, "yyyy-mm-dd")

>>>or

>>>Format$(Date, "mm-dd-yyyy")

>> OK, Mike. I went back and changed to use the Format$ function. That's
>> what I was trying to do in the first place but I didn't realize that I
>> could specify the dashes I thought all I could do was the "mm", "dd"
>> and "yyyy".

>> Thanks.



Sat, 11 Feb 2012 10:36:51 GMT  
 Format a date with dashes?


<cut>

Quote:
> OK, Mike. I went back and changed to use the Format$ function. That's
> what I was trying to do in the first place but I didn't realize that I
> could specify the dashes I thought all I could do was the "mm", "dd"
> and "yyyy".

It could be done that way:

Format$(d,"mm") & "-" & format$(d,"dd") & "-" & format$(d,"yyyy")

or even:

right$("0" & month(d),2) & "-" & Right$("0" & day(d),2) & "-" & year(d)

but doing it in a single Format call usually makes the most sense



Sat, 11 Feb 2012 20:25:23 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. formatting a text box to long date format

2. How to convert number format to date format

3. Date Format Style/format

4. Format for blank res. for zero Date format

5. problem with Format and a user defined date format

6. Date formatting of date type

7. MS-ACCESS query date problem + date formatting in datagrid

8. searching a routine to convert date number in normal format date

9. is date() accepts both UK and US date formats

10. convert Full date from minutes to normal date format

11. Output format of date (using ActiveX Windows date time picker)

12. String date to date format?

 

 
Powered by phpBB® Forum Software