Home > Programming, Servers & Scripts >

Taboola friendly date script (12)


08-13-2020 11:52 PM #1 bhal07 (Member)
Taboola friendly date script

Anyone know how to have an auto updating date script that shows 2 days before the current date? Like instead of 13 August 2020 it would show 11 August 2020...

I tried messing around with this date script i normally use for way too long:

header:

<script type="text/javascript">



/*=============HELPER================*/
var Helper = (function() {


var data = [];


var months = ['January','February','March','April','May','June', 'July','August','September','October','November',' December'];
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday' ,'Friday','Saturday'];


function setDays(d) {
if ((d.constructor !== Array) || d.length !== 7)
return false;


days = d;
}


function setMonths(m) {
if ((m.constructor !== Array) || m.length !== 12)
return false;


months = m;
}


function getDate() {
var now = new Date();
var month = typeof(months[now.getMonth()]) == 'undefined' ? now.getMonth() : months[now.getMonth()];
return (now.getDate()) + " " + month + " " + now.getFullYear();
}


function getMonth() {
var now = new Date();
var month = typeof(months[now.getMonth()]) == 'undefined' ? now.getMonth() : months[now.getMonth()];
return month;
}


function getDay() {
var now = new Date();
var day = typeof(days[now.getDay()]) == 'undefined' ? now.getDay() : days[now.getDay()];
return day;
}


function getDayOfMonth() {
var now = new Date();
var day = ('0' + now.getDate()).slice(-2);
return day;
}


function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}


function findAncestor(element, name) {
while ((element = element.parentElement) && !element.classList.contains(name));
return element;
}


function bindOnQuery(query, callback, action = 'click') {
var elements = document.querySelectorAll(query);
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener(action, callback, false);
}
}


function bindOnId(name, callback, action = 'click') {
var element = document.getElementById(name);
element.addEventListener(action, callback, false);
}


function replaceMarkers(text, markers) {
for (key in markers) {
var search = '%'+ key +'%';
text = text.replace(new RegExp(search, 'g'), markers[key]);
}


return text;
}


return {
setDays: setDays,
setMonths: setMonths,
getDate: getDate,
getDay: getDay,
getMonth: getMonth,
getDayOfMonth: getDayOfMonth,
getUrlParameter: getUrlParameter,
findAncestor: findAncestor,
bindOnQuery: bindOnQuery,
bindOnId: bindOnId,
replaceMarkers: replaceMarkers
};


})();
</script>


callout:

<script>document.write(Helper.getDate())</script>


08-14-2020 10:22 AM #2 matuloo (Legendary Moderator)

The settings of this forum section prevented your thread from being published... corrected it now and should be visible. Let's see if anyone can offer help


08-14-2020 10:47 AM #3 jeremie (Moderator)

Here is a script that will display the date of today with a shift of days_change. It use .toLocaleString, which allows to have the date in the correct language without having to use a lot of arrays with days of the week, months... in each language. Edit: see my post below for more info.

Code:
    <script>
        var date = new Date();
        days_change = -2;
        date.setDate(date.getDate() + days_change);

        const formattedDate = date.toLocaleString("en-GB", {
            day: "numeric",
            month: "long",
            year: "numeric",
        });

        document.write(formattedDate);
    </script>
You can modify the options to display the day of the week, the month with two digits or a word... or even use another calendar. See here for more info:
https://developer.mozilla.org/en-US/...toLocaleString


08-14-2020 10:48 AM #4 matuloo (Legendary Moderator)

Jeremie for the rescue! Thanks a ton man!


08-14-2020 07:23 PM #5 bhal07 (Member)

Wow thanks jeremie that worked right away! Excellent

Quote Originally Posted by jeremie View Post
Here you go:

Code:
    <script>
        var date = new Date();
        days_change = -2;
        date.setDate(date.getDate() + days_change);

        const formattedDate = date.toLocaleString("en-GB", {
            day: "numeric",
            month: "long",
            year: "numeric",
        });

        document.write(formattedDate);
    </script>


08-14-2020 10:19 PM #6 jeremie (Moderator)

One more quick tip as I played a bit more with the script.
It also works with only the 2-letter ISO code of the language.

See for example:

Code:
date.toLocaleString("fr", {
=> 12 août 2020

Code:
date.toLocaleString("es", {
=> 12 de agosto de 2020

Code:
date.toLocaleString("ru", {
=> 12 августа 2020 г.

This means that if you are translating your copy in javascript based on a language parameter passed in the URL by the traffic source / tracker, you can pass directly this parameter to the toLocaleString function and you get the date translated automatically.

To be fair, toLocaleString is not fully supported (96%), but when it is not, it usually displays the date in english
https://caniuse.com/#search=toLocaleString


08-16-2020 08:07 PM #7 bhal07 (Member)

Quote Originally Posted by jeremie View Post
One more quick tip as I played a bit more with the script.
It also works with only the 2-letter ISO code of the language.

See for example:

Code:
date.toLocaleString("fr", {
=> 12 août 2020

Code:
date.toLocaleString("es", {
=> 12 de agosto de 2020

Code:
date.toLocaleString("ru", {
=> 12 августа 2020 г.

This means that if you are translating your copy in javascript based on a language parameter passed in the URL by the traffic source / tracker, you can pass directly this parameter to the toLocaleString function and you get the date translated automatically.

To be fair, toLocaleString is not fully supported (96%), but when it is not, it usually displays the date in english
https://caniuse.com/#search=toLocaleString
Thanks unreal, wicked dude! I actually have a couple French landers I’ll use this for. Very helpful!


Sent from my iPhone using Tapatalk


09-20-2020 04:11 PM #8 mantas (Member)

these 3 lines of code

Quote Originally Posted by jeremie View Post
Here you go:
Code:
        var date = new Date();
        days_change = -2;
        date.setDate(date.getDate() + days_change);
can be reduced to 2 lines

Code:
        days_change = 2;
        var date = new Date(Date.now() - 24 * days_change * 60 * 60 * 1000);


09-21-2020 08:27 AM #9 jeremie (Moderator)

Quote Originally Posted by mantas View Post
these 3 lines of code


can be reduced to 2 lines

Code:
        days_change = 2;
        var date = new Date(Date.now() - 24 * days_change * 60 * 60 * 1000);
Yeah, or to one line

Code:
 
new Date(Date.now() - 1728e5);
But what's the point?


09-21-2020 05:38 PM #10 mantas (Member)

Quote Originally Posted by jeremie View Post
But what's the point?
The loading speed would be faster but probably not a lot lol


09-22-2020 01:54 AM #11 jeremie (Moderator)

Quote Originally Posted by mantas View Post
The loading speed would be faster but probably not a lot lol
Test it... You save 5ms for 10000 runs

Code:
        var days_change = -2;

        var sta1 = performance.now();
        for (var p = 0; p < 10000; p++) {
            var date1 = new Date();
            date1.setDate(date1.getDate() + days_change);
        }
        var end1 = performance.now();

        var sta2 = performance.now();
        for (var p = 0; p < 10000; p++) {
            var date2 = new Date(Date.now() - 24 * days_change * 60 * 60 * 1000);
        }
        var end2 = performance.now();

        console.log('Time 1: ' + (end1 - sta1) + ' ms.');
        console.log('Time 2: ' + (end2 - sta2) + ' ms.');


12-31-2020 07:27 PM #12 iwanttofly (Veteran Member)

So will Taboola allow this? Right now they are denying landers for auto updating date stamp.


Home > Programming, Servers & Scripts >