Results 1 to 2 of 2

Thread: Two digits for year in javascript

  1. #1
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Two digits for year in javascript

    Hello,I have problem because I cannot find the solution with 2 digits for year in javascript. I use this:
    Code:
    var result = today.getFullYear().substring(2, 4);
    I would like help.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The getFullYear() method returns a number. The substring() method can only be applied to a string. The getFullYear value needs to be type converted to a string before using a string method on it, so:

    Code:
    var result = today.getFullYear().toString(10).substring(2, 4);
    Or (probably more efficient):

    Code:
    var result = (today.getFullYear() + '').substring(2, 4);
    Note: In the second example '' is an empty string delimited by single quotes ('). There's nothing inside, not even a space. If in doubt, copy and paste it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Dimi Cost (04-06-2011)

Bookmarks

Posting Permissions

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