Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    convert an eight-digit string to a date variable (Word97SR2)

    How to convert an eight-digit string to a date variable?

    I have the following fields in my document. The {Ask} prompts me for an eight-digit string which I'd key in as 20010731.

    I want the 'week of' field to print as a date format, so where you see InvNumb I really want something that assembles a date vale from my eight-digit value.

    I used to have Week Of Time @ "dddd, MMMM dd, yyyy"}, but that gives me today's date, the date on which I prepare the invoice, and i really want to create my invoices one after another, all on the same day, keying in an appropriate eight-digit string for each one.



    {Ask InvNumb "Invoice Number"}
    Week Of {InvNumb @ "dddd, MMMM dd, yyyy"}
    Invoice Number {InvNumb @ "YYYYMMDD"}

  2. #2
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variable (Word97SR2)

    you could use ...

    int(YYYYMMDD/10000) to return the year
    mod(YYYYMMDD,100) to return the date
    int(mod(YYYYMMDD,10000)/100) to return the month

    to three separate variable and then go from there.
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variable (Word97SR2)

    True; but it's the "go from there" that has me stumped. I see no equivalent of a Date(yy,mm,dd) function to return a date-value, which I believe I need for the date formatting capabilities of the date field.

    I can use the digit-portions of the 8-digit string to yield formatted numbers, but if I want also to produce a date as "Tuesday August 8th 2001" I need date values.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: convert an eight-digit string to a date variable (Word97SR2)

    Try the VBA CDate conversion function. That will convert a date string to a date.
    Charlotte

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variable (Word97SR2)

    > VBA CDate conversion function

    Thanks, Charlotte, indeed it will. I'm struggling, though, to obtain a Word-only solution.

    With Timbo's help I can now obtain the number of days difference between today's date {DATE} and the invoice number.

    If I could just add that difference to today's date {DATE} I'd be set (because then I'd have a DATE adjusted, still a DATE, and could use the DATE formatting options within the field.

    I'm also re-perusing Woody's "TUG".

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: convert an eight-digit string to a date variable (Word97SR2)

    <hr>I'm struggling, though, to obtain a Word-only solution.<hr>
    Unless you enjoy pain, why? That's what VBA is there for.
    Charlotte

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variable (Word97SR2)

    > That's what VBA is there for.

    I disagree. Starting with Woody's references (The Underground Guide p106) Fields are seen as a powerful and versatile scheme of getting things done within the document.

    I enjoy VBA (I am, after all, British by birth) yet I balk at having VBA code that will have to be invoked by the user to do a simple calculation. Especially as Word's Field calculations seem so darn good.

    My original posting in this thread was effectively asking if I'd overlooked something; apparently not. There appears to be no string-to-date conversion as there is in Excel and several other "languages".

    I can do it in VBA in my head while I'm typing this. So odd that it was left out of the Fields arsenal.

  8. #8
    New Lounger
    Join Date
    Aug 2001
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variable (Word97SR2)

    If you use the 'int' and 'mod' functions suggested by Timbo, and then enclose all these fields in a 'quote' field, with the date formatting at the end, does this not give you everything you need within Word?
    see attached,

    Oli
    Attached Files Attached Files

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variable (Word97SR2)

    Oli! You have set the keystone in place. Thank you. I felt sure it ought be possible in Field codes. Not as elegant as a ConvDate field, but more versatile.

    I have attached my vserion of the document and pasted the explanations here.



    ask field

    Invoice Number: 20010828


    Year Portion: 2001

    Month Portion: 8

    Day portion: 28

    Hard-coded: Invoice Date: Tuesday, August 28, 2001

    Invoice Date: Tuesday, August 28, 2001



    Explanation of underlying code:



    { ASK InvNumb "Type an 8-digit string"} This is the ask field. Select the entire document (Ctrl-A) and tap the F9 function key. You will be prompted for a string. Type in the eight digits 20010828 which represent Tuesday 28th August 2001 in a numeric form.

    Invoice Number: { InvNumb } Your response to the {ASK} is stored in a bookmark variable "InvNumb", from which we will extract our data. Here you see the 8-digit string being used purely as an identifier for the Invoice document.

    Year Portion: { = int(InvNumb /10000)} Here is Timbo's work. We extract the four-digit year portion.

    Month Portion: {=int(mod(InvNumb,10000)/100) } Here is Timbo still at work. We extract the two-digit month portion.

    Day portion:{ =mod(InvNumb,100) } The indefatigable Tmbo extracts the two-digit date portion.

    Hard-coded: Invoice Date: { quote "2001 08 28" @ "dddd, MMMM dd, yyyy"} Here I have included an wayside rest stop. Oli chimed in with the use of the QUOTE field which I'd not met before. Word97's help screens gave me some good ideas. Heh heh. Meanwhile, back at the ranch, you can experiment with this particular field. Try eliminating the spaces between the digit strings thus "20010828". Try using slashes instead thus "2001/08/28". Interesting, no? Try eliminating the double-quote characters. Aaaaargh!

    Invoice Date:{ quote "{ = int(InvNumb /10000)} {=int(mod(InvNumb,10000)/100) } { =mod(InvNumb,100) }" @ "dddd, MMMM dd, yyyy"} OK. Here's the result I wanted, and here I have it, thanks to Timbo and Oli. I took the hard-coded example above, and, being careful to retain those double-quote characters, I replaced the 4-digit and 2-digit strings with their correspoonding fields from Timbo's work.

    Voila!, as I used to say when I worked in Paris. Ole! In Alicante. Oli! in the Word forum.
    Attached Files Attached Files

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variable (Word97SR2)

    > { ASK InvNumb "Type an 8-digit string"} This is the ask field. Select the entire document (Ctrl-A) and tap the F9 function key.

    I should add that the Input box for the Ask field is awkward. The default behaviour on tapping the Enter key is to enter a CR/LF in the text box, rather than to confirm as if the OK button had been clicked.

    This will trip you up from time to time. The side effects include awkward spacing in your document, because that extra CR/LF in your text box appears as extra paragraph spacing in your document wherever you make use of the entire bookmark (as in my example where I produce the Invoice Number as an 8-character string). You'll see the extra spacing, and you'll try to be rid of it by choosing Format, Paragraph, but to the best of my knowledge, there's no way to be rid of it, execept by re-issuing the {Ask} (Ctrl-A, F9)

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variab

    I have developed (attached) a scam^H^H^H^H scheme for converting an eight-character digit string to an ordinalized (?) date format.

    I thought it couldn't be done, but it can!

    Wednesday is the source data for the mail merge
    Thursday is a stepped set of exercises in field codes.
    Just run the mail merge and watch the results.

    I still have NOT solved my original problem of converting an eight-character dtring of digits to an internal date format.
    Attached Files Attached Files

  12. #12
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: convert an eight-digit string to a date variab

    Hi Chris,

    Converting an 8-digit string into a date is really quite straightforward. My <post#= 249902>post 249902</post#> shows how. Converting such a string the show the day# as an ordinal is a bit more effort, but doesn't have to be as complex as your solution. See the attachment.

    Cheers
    Attached Files Attached Files
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variab

    Macropod, thank you, and yes, you must recognise the source of my efforts.

    I checked your new example for ordinals, and it looks/works great too, but as far as I can see we are still dealing with strings-of-characters.

    These solutions implement a date-looking string from an 8-character date string (which is what the client wants), but I still can't see a way of converting an 8-character string into a Date Serial value.

    Some reflection is steering me towards the thought that perhaps I'm looking for the wrong concept. Word97 has DatePrinted, DateSaved etc., but no real date arithmetic; if it did, we'd see a DateValue such as is found in Excel97.

    Again, thanks; your code provides an ordinal date string, which is going to look great on/in the document. I just wish we had a string-to-serial-date-value function.

  14. #14
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: convert an eight-digit string to a date variab

    <P ID="edit" class=small>(Edited by macropod on 01-May-04 13:25. Direct string to serial# conversion added)</P>Hi Chris,

    Converting a date to a serial value is as 'easy' as:
    {QUOTE{SET a{=INT((14-{DATE @ M})/12)}}{SET b{={DATE @ yyyy}+4800-a}}{SET c{={DATE @ M}+12*a-3}}{SET d{DATE @ d}}{SET DateValue{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045-2415019}}{DateValue # ,0}}
    This uses the conversion of a Gregorian date to a Julian day number, as demonstrated in my DateCalc document, and simply deducts the Julian day number for 30/12/1899 (2,415,019) to get the integer-equivalent of the MS DateSerial value.

    Now that you've got the field coding to convert a string to a date and to convert a date to a dateserial value, you shouldn't find it too hard to combine the two. In fact, you don't really need to convert the string to a date. For example, try:
    {QUOTE{ASK Data Date @ YYYYMMdd}"The serial # for "{QUOTE "{=Data # "0000'-'00'-'00"}" @ "dddd, d
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  15. #15
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: convert an eight-digit string to a date variab

    > Converting a date to a serial value is as 'easy' as:

    Well, thank you. By the time I'd got this one embedded in Field codes, it was time for me to be embedded. I'll tackle the edited appendage later today (Sunday!)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •