-
Jan 5th, 2023, 05:32 AM
#1
[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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jan 9th, 2023, 03:34 PM
#2
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:
- You are assuming that balance is not null or undefined
- You are assuming that balance.CardExpiry is not null or undefined
- You are assuming that balance.CardExpiry is a String and has at least 6 characters
- You are assuming that everything after the first 6 characters of balance.CardExpiry can be converted to a number
- 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.
-
Jan 11th, 2023, 06:45 AM
#3
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jan 11th, 2023, 09:57 AM
#4
Re: [RESOLVED] One line subtract 1 day
Ah, yeah then that changes things.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|