Inserting date/time in certain format?

S

StargateFan

I put the icons for date and time, which are separate, onto my toolbar
yesterday. But I got a "2005-02-10" and "1:30 PM" result. I need to
customize to a toolbar icon something like this instead:

"2005.02.10.Th., 13h30" which is format we use at office.

Is there any way to do this? Or perhaps a macro that I can attach to
a toolbar? Any ideas?

Thanks much! :eek:D
 
G

Greg

If you could live with the standard day abbreviations you could use:

Sub DateStamp2()
' Inserts current date
Dim timeStr As String


timeStr = Format(Date, "yyyy.mm.dd.ddd") & "., " & Format(Time, "HH") &
"h" & Format(Time, "n") & " "
Selection.InsertBefore (timeStr)
Selection.Collapse Direction:=wdCollapseEnd
End Sub


Otherwise you would need a pretty complicated IF expression to convert
say Fri to Fr, Sun to Su, etc.
 
M

Margaret Aldis

Hmm - you could set up a DATE field with the Date-Time picture you want,
save it as AutoText and put that on the toolbar. But you'd need to select
and press Ctrl-Shift-F9 or Ctrl-6 to stop it updating to the current date
when you open the document again.

Or you could write a one-line macro using the InsertDateTime method -
something like

Selection.InsertDateTime _
DateTimeFormat:="yyyy'.'MM'.'dd'.'ddd'.,' HH'h'mm", InsertAsField:=False

I don't believe you can get the Th abbreviation - ddd gives you three
letters. (See Help topic for Date Time picture.)
 
G

Graham Mayor

Not that complicated :)

Sub DateStamp2()
' Inserts current date
Dim timeStr, dayStr As String

dayStr = Left(Format(Date, "ddd"), 2)
timeStr = Format(Date, "yyyy.mm.dd.") & dayStr & "., " _
& Format(Time, "HH") & "h" & Format(Time, "n") & " "
Selection.InsertBefore (timeStr)
Selection.Collapse Direction:=wdCollapseEnd
End Sub

http://www.gmayor.com/installing_macro.htm
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Suzanne S. Barnhill

See http://word.mvps.org/FAQs/TblsFldsFms/DateFields.htm for help in
constructing a field to your requirements. You'll doubtless need a
concatenation of several fields, and, as Margaret pointed out, your
abbreviation for the day of the week is not supported by Word. When you have
arrived at a string of fields that works, you can save it as an AutoText
entry. Note that you probably don't want a Date field but more likely
CreateDate.

Note that the Date and Time fields by default use the default short or long
date format you have defined in Windows Control Panel | Regional Options.
 
S

StargateFan

Not that complicated :)

Sub DateStamp2()
' Inserts current date
Dim timeStr, dayStr As String

dayStr = Left(Format(Date, "ddd"), 2)
timeStr = Format(Date, "yyyy.mm.dd.") & dayStr & "., " _
& Format(Time, "HH") & "h" & Format(Time, "n") & " "
Selection.InsertBefore (timeStr)
Selection.Collapse Direction:=wdCollapseEnd
End Sub

EXCELLENT! I had to play a bit with some spaces as the ng puts breaks
in long lines <g>, but got the code working! I knew this could be
done. I have the same non-standard format in Excel from vba coding
also so kindly given to me but about a year ago or more now.

There is only one thing I would change, if it's possible? Can we have
leading zero for the time? i.e., my freeware systray clock showed
23h09 a few moments ago.

The time portion of this Word macro, however, writes out the same
time as 23h9 instead of 23h09, so no leading zero in the minutes.
Also, if in the a.m., would need format like 02h02 instead of 2h2 so
that, in other words, the hours also have a leading zero. Can this
excellent code above be modified to reflect that?

Thanks much!
 
S

StargateFan

See http://word.mvps.org/FAQs/TblsFldsFms/DateFields.htm for help in
constructing a field to your requirements. You'll doubtless need a
concatenation of several fields, and, as Margaret pointed out, your
abbreviation for the day of the week is not supported by Word. When you have
arrived at a string of fields that works, you can save it as an AutoText
entry. Note that you probably don't want a Date field but more likely
CreateDate.

Note that the Date and Time fields by default use the default short or long
date format you have defined in Windows Control Panel | Regional Options.

Graham Mayor very kindly provided a simple yet elegant code to do just
that <g>. This time format is non-standard in Excel, also, yet I have
code to do this very same thing. Granted, code is vastly different
than the one Graham provided, but it does the same job. That's why I
knew it would be impossible. <g>

Cheers! :eek:D
 
G

Graham Mayor

am hours should be OK as it stands. For the minutes you need to add a second
'n' to the time mask ie

Sub DateStamp2()
' Inserts current date
Dim timeStr, dayStr As String

dayStr = Left(Format(Date, "ddd"), 2)
timeStr = Format(Date, "yyyy.mm.dd.") & dayStr & "., " _
& Format(Time, "HH") & "h" & Format(Time, "nn") & " "
Selection.InsertBefore (timeStr)
Selection.Collapse Direction:=wdCollapseEnd
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

StargateFan

Graham Mayor very kindly provided a simple yet elegant code to do just
that <g>. This time format is non-standard in Excel, also, yet I have
code to do this very same thing. Granted, code is vastly different
than the one Graham provided, but it does the same job. That's why I
knew it would be impossible. <g>

<g> ... That should read "knew it would be POSSIBLE ... <lol>

[snip]
 
S

Suzanne S. Barnhill

I figured you meant it would be impossible for *you* (but possible thanks to
Graham). <g>



StargateFan said:
Options.

Graham Mayor very kindly provided a simple yet elegant code to do just
that <g>. This time format is non-standard in Excel, also, yet I have
code to do this very same thing. Granted, code is vastly different
than the one Graham provided, but it does the same job. That's why I
knew it would be impossible. <g>

<g> ... That should read "knew it would be POSSIBLE ... <lol>

[snip]
 
S

StargateFan

I figured you meant it would be impossible for *you* (but possible thanks to
Graham). <g>

StargateFan said:
On Fri, 11 Feb 2005 08:56:36 -0600, "Suzanne S. Barnhill"

See http://word.mvps.org/FAQs/TblsFldsFms/DateFields.htm for help in
constructing a field to your requirements. You'll doubtless need a
concatenation of several fields, and, as Margaret pointed out, your
abbreviation for the day of the week is not supported by Word. When you have
arrived at a string of fields that works, you can save it as an AutoText
entry. Note that you probably don't want a Date field but more likely
CreateDate.

Note that the Date and Time fields by default use the default short or long
date format you have defined in Windows Control Panel | Regional Options.

Graham Mayor very kindly provided a simple yet elegant code to do just
that <g>. This time format is non-standard in Excel, also, yet I have
code to do this very same thing. Granted, code is vastly different
than the one Graham provided, but it does the same job. That's why I
knew it would be impossible. <g>

<g> ... That should read "knew it would be POSSIBLE ... <lol>

[snip]
 
S

StargateFan

Not that complicated :)

Sub DateStamp2()
' Inserts current date
Dim timeStr, dayStr As String

dayStr = Left(Format(Date, "ddd"), 2)
timeStr = Format(Date, "yyyy.mm.dd.") & dayStr & "., " _
& Format(Time, "HH") & "h" & Format(Time, "n") & " "
Selection.InsertBefore (timeStr)
Selection.Collapse Direction:=wdCollapseEnd
End Sub
[snip]

There is only one thing I would change, if it's possible? Can we have
leading zero for the time? i.e., my freeware systray clock showed
23h09 a few moments ago.

The time portion of this Word macro, however, writes out the same
time as 23h9 instead of 23h09, so no leading zero in the minutes.
Also, if in the a.m., would need format like 02h02 instead of 2h2 so
that, in other words, the hours also have a leading zero. Can this
excellent code above be modified to reflect that?

Any way to get two leading zeros?

Cheers.
 
G

Graham Mayor

StargateFan said:
Any way to get two leading zeros?

*Two* leading zeros? You can get any layout you want by applying varations
to the format included in the code. Where do you want the two zeros and
under what circumstances do you want the extra ones? The revised code I
posted earlier will match your original request for one leading zero where
appropriate.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

StargateFan

*Two* leading zeros? You can get any layout you want by applying varations
to the format included in the code. Where do you want the two zeros and
under what circumstances do you want the extra ones? The revised code I
posted earlier will match your original request for one leading zero where
appropriate.

Darn, must have missed a post. I'll see if re-dl headers will bring
it back. Re two leading zeros, one for hours and one for minutes,
i.e., instead of 2h2 (for 2 a.m. and 2 minutes), 02h02.

Thanks. I'll go look for the extra post. Agent must have missed it
somehow even though thread being watched. Cheers!
 
S

StargateFan

am hours should be OK as it stands. For the minutes you need to add a second
'n' to the time mask ie

Sub DateStamp2()
' Inserts current date
Dim timeStr, dayStr As String

dayStr = Left(Format(Date, "ddd"), 2)
timeStr = Format(Date, "yyyy.mm.dd.") & dayStr & "., " _
& Format(Time, "HH") & "h" & Format(Time, "nn") & " "
Selection.InsertBefore (timeStr)
Selection.Collapse Direction:=wdCollapseEnd
End Sub

Ah, here it is! Yes, that seems to be working just fine. I've made
the change you advise by putting the extra "n" in the time format
part.

Thank you! This little macro is going to make life much easier.
 

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

Top