Results 1 to 4 of 4

Thread: [RESOLVED] One line subtract 1 day

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,380

    Resolved [RESOLVED] One line subtract 1 day

    Hi.
    Code:
       let $dl = $('<dl>').addClass('dl-horizontal');
                $dl.append($('<dt>').text('Ημ. Λήξης'))
                $dl.append($('<dd>').append($('<span>').addClass('pull-right').text(new Date(parseInt(balance.CardExpiry.substr(6))).toLocaleDateString("el"))))
    is there a way to subtract one day from this in the same line? The .CardExpiry is in the form of /Date(1704060000000)/

    Edit:
    OK never mind, I fixed it with more lines (if doable with one line,just for educational reason do let me know)

    Code:
                var today = new Date(parseInt(balance.CardExpiry.substr(6)));
                today.setDate(today.getDate() - 1);
                let $dl = $('<dl>').addClass('dl-horizontal');
                $dl.append($('<dt>').text('Ημ. Λήξης'))
                $dl.append($('<dd>').append($('<span>').addClass('pull-right').text(today.toLocaleDateString("el"))))
    Last edited by sapator; Jan 5th, 2023 at 08:19 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,254

    Re: [RESOLVED] One line subtract 1 day

    Personally, I wouldn't even be doing it with the two extra lines you have added. You are assuming several things which could make it difficult to debug if something goes wrong, for example:
    1. You are assuming that balance is not null or undefined
    2. You are assuming that balance.CardExpiry is not null or undefined
    3. You are assuming that balance.CardExpiry is a String and has at least 6 characters
    4. You are assuming that everything after the first 6 characters of balance.CardExpiry can be converted to a number
    5. You are assuming that the numeric value of everything after the first 6 characters of balance.CardExpiry can be converted to a date


    Even though it makes the code a bit lengthier, I would suggest adding some validation checks in there and do something (console.error?) if one of the validations fail.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,380

    Re: [RESOLVED] One line subtract 1 day

    Just to be clear. The code is not mine and there are minimum changes to be done as it's more of a notepad editor fixes.
    So yeah, I hear you but it is what it is.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,254

    Re: [RESOLVED] One line subtract 1 day

    Ah, yeah then that changes things.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

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



Click Here to Expand Forum to Full Width