Skip to content

Commit

Permalink
🔨 better date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
4silvertooth committed Feb 5, 2021
1 parent 68b9f59 commit 8a06af5
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions main.tis
Expand Up @@ -5,27 +5,13 @@ $(#quit) << event click {
view.close();
}

function pad(digit) {
const d = digit.toString();
if (d.length == 1) return "0" + d;
if (d.length == 0) return "00";
return d;
}

function updateDate() {
const date = new Date().toLocaleString(#time);
const regex = /(\d+):(\d+):(\d+) ?(.*)?/;
const matches = date.match(regex);
const [match, hours, minutes, seconds, meridian] = matches;
const h = pad(hours);
const m = pad(minutes);
const H = root.TIME_FORMAT == "12h"
? h
: pad(new Date().hour);
const MERIDIAN = root.TIME_FORMAT == "12h"
? String.$( {meridian})
: "";
$(#time).text = String.$({H}:{m}{MERIDIAN});
if(root.TIME_FORMAT == "12h") {
$(#time).text = new Date().toString("%I:%M %p");
}
else {
$(#time).text = new Date().toString("%R");
}
return true;
}

Expand Down

1 comment on commit 8a06af5

@4silvertooth
Copy link
Collaborator Author

@4silvertooth 4silvertooth commented on 8a06af5 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const date = new Date().toLocaleString(#time);

This will give time in different locale in every country, if the country uses time in format 10.30 AM or 10:30 AM or 10-30 AM and what not, so

const regex = /(\d+):(\d+):(\d+) ?(.*)?/;

this won't work in every country, as it didn't in mine, because my time locale was with a . as separator and not :

In Sciter Date.toString will give RFC-822 string representation of date object which will be same in all Locale. Which can be formatted using strftime specifiers which can be used to take care of all the padding and locale. Short and sweet also reduces the load of every second computation.

Please sign in to comment.