lastModified script with DATE & TIME

Sorry, I’m still just waking up, and it’s nearly time for bed.

That s = line should not be in the date_ddmmyy function at all.

This s - line is now off. (LINE 253)

The correct one is on LINE 305.

Although the DATE/ TIME is not produced, at least there is no error from the JScr console in Safari.

Hope you can find the culprit :slight_smile:

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

/

with best regards, Omar KN, Stockholm, Sweden

I see that you are using d1 in the code, which was no longer needed.
Remove the lines that use d1, and that should resolve the matter.

You should then only have a date year problem to fix.

Done that, and now it looks like this:

LastMod 4120-09-23 05:36 UTC

/okn
(Kind of wonder what we’ll be doing this year … )

Yep, You’ll need to look at where getUTCFullYear is used.

=SOLVED_!

LastMod 2020-09-23 05:36 UTC

But why did the original coder have to add + 2100 ?

Anyway, now it’s working!

/okn

1 Like

There can be many reasons, and none of them matter now at this stage.

Thank you for everything in all those steps, I think I learned a lot.

Where would you recommend to deposit this script for others to find it?

GitHub ?

/

with best regards, Omar KN, Stockholm, Sweden

I would recommend using the Moment library instead.

For example:

var lastModified = new Date(document.lastModified);
var UTCmoment = moment(lastModified).tz("utc");
var s = UTCmoment.format("YYYY-MM-DD HH:MM") + " UTC";

This appears to be a more compact form.

But it’s probably necessary to rewrite the code from the start,
or just replace the old
var s = "Unknown";
with
var s = UTCmoment.format …
?

Is there ever a situation where Unknown is shown on the screen? When is document.lastModified ever not a date?

ok so it can be replaced.

Then I suppose that
function date_ddmmyy(date)
and
function time_hhmm_utc(date)

will have to replaced by the new moment var definitions?!

/

You can download moment.min.js from the Moment site, and load that on the HTML page too.

So now downloaded the moment.min.js
and have it here:
<script type="text/javascript" src="moment.min.js"></script>

The simple script is then this.

<script type = "text/JavaScript" language = "JavaScript">
    <!--

function lastModifiedUTCDateTime(?) {
var lastModified = new Date(document.lastModified);
var UTCmoment = moment(lastModified).tz("utc");
var s = UTCmoment.format("YYYY-MM-DD HH:MM") + " UTC";

    return s;
}

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

// -->
</script>

Sorry for this stone-age attempt to a code!

/okn

Got this error message (for tests4m moment, link is below ):

var UTCmoment = moment(lastModified).tz("utc");
is not a function
= LINE249

of file:
https://www.livingislam.org/e/tests4m.html

/okn

If we can return to the working script in test4a2
where

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;

I just now realise that the time which is rendered is the time when the webpage is loaded or reloaded.

The intention was to get the date & time when the page was saved (lastModified) by the webadmin before publishing it.

Is this even possible with JavaScript?

Sorry if I didn’t express myself better.
I’m afraid that this is the same behaviour not only for the time, but also for the date.
I’ll check this in the morning…

/

with best regards, Omar KN, Stockholm, Sweden

Yes - when it comes to timezones, that is such a complex matter that a separate download is required. https://momentjs.com/timezone/

At the https://www.livingislam.org/e/tests4a.html page the date information in the headers is:

Date: Wed, 23 Sep 2020 20:39:54 GMT
Last-Modified: Wed, 23 Sep 2020 11:40:25 GMT

On another load of the page, the dates in the headers are:

Date: Wed, 23 Sep 2020 20:41:01 GMT
Last-Modified: Wed, 23 Sep 2020 11:40:25 GMT

And the page reliably shows the 11:40 time.

The evidence that I’m seeing is very different from what you stated.

Can you please supply some kind of evidence to support your statement?

You can test it here, it’s just before the footer, to the left.
https://www.livingislam.org/religion-islam/soul-nafs.php

This page is built with RapidWeaver (RW), the javascript is added into a HTML stack - in the BODY.

However in this page:
https://www.livingislam.org/e/tests4a.html

the time ‘sticks’, doesn’t change.

So it has to do with RW how it interprets the code?!

/

with best regards, Omar KN, Stockholm, Sweden

The lastModified part is correct, but not what you expected. PHP pages are generated live as people access the page, so they are always newly modified.

I recommend using PHP to set the lastmodified header to the file modification date instead.