lastModified script with DATE & TIME

Need help with a DATE- TIME lastModified script such as this:

https://www.abc.se/home/m9783/ir/x/_scripts/oldlastmod.html

but where TIME is also included.

like so:

LastMod 20200917 06:34 UTC

/

with best regards, Omar KN, Stockholm, Sweden

Well you have a date_ddmmmyy() function, so it makes sense to also use a time_hhmm_utc() function.

That means, updating the section of the code that shows the date:

    // s = " " + date_ddmmmyy(new Date(d1));
    var date = new Date(d1);
    s = " " + date_ddmmmyy(date) + " " + time_hhmm_utc(date);

The time_hhmm_utc() function gets the utc hours and minutes, uses padStart to add a leading zero if need be, and returns the combined string.

function time_hhmm_utc(date) {
    var utcHours = date.getUTCHours();
    var utcMinutes = date.getUTCMinutes();
    var hours = String(utcHours).padStart(2, "0");
    var minutes = String(utcMinutes).padStart(2, "0");
    return hours + ":" + minutes + " UTC";
}

Thank you Paul.

I tried to implement those changes and called them

  • new entry1

  • new entry2

and 2 of mine

changed var mmm to var mm and Sep to 09

  • new entry3

  • new entry4 changed mmm to mm

it doesnā€™t seem to work.

Please have a look at the updated version 02 here:

http://www.abc.se/home/m9783/ir/x/_scripts/lastMod02.html

The originial was here:

https://www.abc.se/home/m9783/ir/x/_scripts/oldlastmod.html

/

with best regards, Omar KN, Stockholm, Sweden

Please supply a link to a page that uses the scripting code, so that effective investigation may occur.

The script is in this page

https://bit.ly/2RJPTkf

@ ā†’ lastmodified JavaScript:

/okn

The browser console shows some errors with your code. I recommend that you start by using that information to fix problems in your code.

Hi and good day,

Got rid of all the other scripts, so now only the lastModified script is left (in tests2.html)*** and 1 error:

return ā€œā€ + << it should be inside the function.
// so it has to be placed inside the function - but how?

***https://www.livingislam.org/e/tests2.html

Join that return line together with the next line below it. That should fix that problem.

At one point (lost it now) I got the ff:

Last modified undefined 10:22 UTC

but not the date,

then Iā€™m not sure about mmm or mm, because I only want 2 digits for the month:

LINE253: function time_hhmm_utc(date)
or function time_hhmmm_utc(date)
ā€¦
LINE288: var mmm = or var mm
ā€¦
LINE291: (9==m)?'09':

But this:
LINE303: SyntaxError: Unexpected keyword ā€˜returnā€™

Yes, letā€™s start with this one. The problem there is that there is a newline after the return plus symbol. Join that return line together with the next line below it.

Even when in 1 line, still:
LINE303: SyntaxError: Unexpected keyword ā€˜returnā€™

I thought to set the return (LINE303) after the function date_ddmmmyy(date) and inside its brackets } but no success.

https://www.livingislam.org/e/tests2.html

update:

I started with a new file (tests3 = https://www.livingislam.org/e/tests3.html )
same as in the morning today.

Then
I put return in 1 line
LINE299

return "" + (y<10?"0"+y:y) + ", " + mm + ". " + (d<10?"0"+d:d);
}

but got the error:
SyntaxError: Return statements are only valid inside functions.

While the
date_ddmmmyy(date)
function LINE250 has an end bracket
}
correct?!

So return is not included in this function?

/

with best regards, Omar KN, Stockholm, Sweden

Update - some success:

In the new file (tests3b = https://www.livingislam.org/e/tests3b.html)
I commented out the min-function

LINE259-265
+
LINE321
+
commented out the } after date_ddmmmyy(date)
function LINE255

So now the date is showing alright!

LastMod 2020-09-21

So far so good!

/

with best regards, Omar KN, Stockholm, Sweden

Similarly,

when commenting out the date function,

I get the time :

LastMod 17:46 UTC

But how to combine 2 functions with correct brackets?

/

with best regards, Omar KN, Stockholm, Sweden

Looking at your code, it seems that you have inappropriately put my time function inside of the date function.

Please move the time function out of the date function. It will then also be easier for you to find the inappropriate early closure of the date function.

Edit: oh good, thatā€™s been resolved. Iā€™ll investigate further after transit has completed.

There are a number of issues with the code. Some of them fatal, others less so.

Time function

Move my time function out of the date function and put it above the date function. You can then safely uncomment the time function.

Opening braces on same line

Move the opening brace of each function up to the same line as the function. Dropping it down to the next line is for a different programming language. JavaScript has a different set of standards than some other programming languages.

For example, with the date function:

// function date_ddmmmyy(date)
// {
function date_ddmmmyy(date) {

In these code examples Iā€™ve used a comment to help show the code before the change. Donā€™t copy those comments to your updated code, theyā€™re not needed at all.

Clean up the code

Run the code through beautifier.io. You donā€™t need me to tell you off about tabs or spaces or how much to put on either side of operator symbols. That website helps to automatically fix several different common code formatting troubles.

Confused function name

You have changed parts of the date function so that it doesnā€™t show the month in text, and instead shows the date as a number. You need to rename the function name so that it more accurately explains what it does. The mmm in the function name is inappropriate now, and should be changed to be mm instead.

Here is the old code:

// format date as yy-mmm-dd
// example: 99, Jan.12
//
function date_ddmmmyy(date) {
...
        s = " " + date_ddmmmyy(date) + " ";

And here is the updated code:

// format date as yy-mm-dd
// example: 99-01-12
//
function date_ddmmyy(date) {
...
        s = " " + date_ddmmyy(date) + " ";

Remove the text months

You have no need for the text months in your code now. You can completely remove this section:

// code to remove

    // changed var mmm to var mm and Sep to 09  - - - new entry3
    var mm =
        (1 == m) ? 'Jan' : (2 == m) ? 'Feb' : (3 == m) ? 'Mar' :
        (4 == m) ? 'Apr' : (5 == m) ? 'May' : (6 == m) ? 'Jun' :
        (7 == m) ? 'Jul' : (8 == m) ? 'Aug' : (9 == m) ? '09' :
        (10 == m) ? 'Oct' : (11 == m) ? 'Nov' : 'Dec';

At the end of the date function, you can simplify it by doing the padding before returning the values.

    // return "" + (y < 10 ? "0" + y : y) + "-" + mm + "-" + (d < 10 ? "0" + d : d);
    y = (y < 10 ? "0" + y : y);
    m = (m < 10 ? "0" + m : m);
    d = (d < 10 ? "0" + d : d);
    return y + "-" + m + "-" + d;

Make clever code easier to understand

This next bit of code is too complex to easily understand.

    if (0 != (d1 = Date.parse(lmd))) {

Thatā€™s just checking if you get a non-falsey value, and is much simplified by replacing it with a simpler check instead:

    // var d1;
    ...
    // if (0 != (d1 = Date.parse(lmd))) {
    if (Date.parse(lmd)) {
        // var date = new Date(d1);
        var date = new Date(lmd);

Restore the time display

Now that the time function is accessible, we can use that here too.

        // s = " " + date_ddmmyy(date) + " ";
        s = " " + date_ddmmyy(date) + " " + time_hhmm_utc(date);

Move ending parenthesis to new line

The last thing that should be done to clean up this code, is to fix the formatting of the following code:

document.write(
    "LastMod " +
    date_lastmodified());

Thereā€™s no good reason for that to be on multiple lines, so bring it all up to be on the same line.

document.write("LastMod " + date_lastmodified());

Summary

That is a the minimum of what I would do to improve the code, so that it works and is easier to understand.

There is no doubt more that I would find more by scanning through the updated code once again, but this should be enough for now.

1 Like

Hi and good day Paul,

I ran it through the Beautifier.io


Now thereā€™s 1 error

RangeError: Maximum call stack size exceeded.

for

function date_ddmmyy(date) {
    var d = date.getUTCDate();
    var m = date.getUTCMonth() + 1;
    var y = date.getFullYear() + 2100;

    s = " " + date_ddmmyy(date) + " ";

for the LINE s = ā€¦

which probably should be further down?!

the updated code is here:
https://www.livingislam.org/e/tests3t-4.html

/

with best regards, Omar KN, Stockholm, Sweden

Is that the only thing that you did from my list?

No I did every step -

It cannot be that I have

s = " " + date_ddmmyy(date) + " ";
on LINE 253

and later
s = " " + date_ddmmyy(date) + " " + time_hhmm_utc(date);
on LINE 305

?

1 Like

Correct, it cannot be that. Letting me take a look at the code might be handy though.