Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin2953 committed May 13, 2022
0 parents commit 7c1bcf7
Show file tree
Hide file tree
Showing 19 changed files with 19,706 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const sass = require("sass");
const qrcode = require("qrcode-terminal");
const devIp = require("dev-ip");

module.exports = function (eleventyConfig) {
eleventyConfig.setQuietMode(true);
eleventyConfig.addPassthroughCopy("js/lib");
eleventyConfig.addWatchTarget(".");

eleventyConfig.setServerOptions({
port: 5555,
showAllHosts: true,

// https: {
// key: "./localhost.key",
// cert: "./localhost.cert",
// },

// Change the name of the special folder name used for injected scripts
// folder: ".11ty",
});
let devip = "http://" + devIp()[0] + ":5555";
if (devIp()[0]) { qrcode.generate(devip, { small: true }) }

eleventyConfig.addFilter("sass", code => {
return sass.compileString(code).css.toString();
});

return {
dir: {
output: "dist"
}
};
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
tmp
72 changes: 72 additions & 0 deletions index.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--।। श्री ।।-->
<!doctype html>
<html lang="en-IN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Kalc.Tools DateDiff Test</title>
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect x='0' y='0' width='100' height='100' fill='%23f00'/%3E%3Ctext x='-.06em' y='.97em' font-size='80'%3E✳️%3C/text%3E%3C/svg%3E" type="image/svg+xml">
<link rel="stylesheet" href="/main.css">
<script type="module" src="/main.js"></script>
</head>
<body>
<header>
<details>
<summary>
<a href="https://kalc.tools" target="_blank">
<img decoding="sync" loading="eager" src="/logo.svg" width="108" height="40" alt="Kalc.Tools Logo" title="Backlink">
</a>
</summary>
<p>
<kbd>⇅</kbd> => <kbd>Enter</kbd> || <kbd>Spacebar</kbd> || <kbd>Left Click</kbd> = +1 day
<br>
<kbd>⇅</kbd> => <kbd>Shift</kbd> + (<kbd>Enter</kbd> || <kbd>Spacebar</kbd>) || <kbd>Right Click</kbd> = -1 day
</p>
<hr>
<p>
<kbd>🦀</kbd> => <kbd>Enter</kbd> || <kbd>Spacebar</kbd> || <kbd>Left Click</kbd> = <code>"tr:nth-child(15) ~ *".remove()</code>
<br>
<kbd>🦀</kbd> => <kbd>Shift</kbd> + (<kbd>Enter</kbd> || <kbd>Spacebar</kbd>) || <kbd>Right Click</kbd> = <code>"tr".remove()</code>
</p>
</details>
</header>

<div>
<samp id="local_date" title="Locale Date"></samp>
<form>
<label>Start Date<input type="date" id="start_input" required autofocus></label>
<label>End Date<input type="date" id="end_input" required></label>

<button type="button" id="start_oper" class="swap">⟵ ⟶</button>
<button type="button" id="end_oper" class="swap">⟵ ⟶</button>

<input type="submit" tabindex="-1">

<button type="button" id="trim" accesskey="c">🦀 Table</button>
<button type="button" id="swap" accesskey="x">🔁 Dates</button>
</form>
</div>

<output>

<table>
<thead>
<tr>
<th><code>startDate</code></th>
<th><code>endDate</code></th>
<th><code>Temporal</code></th>
<th><code>fcTemporal</code></th>
<th><code>r3</code></th>
<th><code>r4</code></th>
<th>⭕</th>
<th>🎂</th>
</tr>
</thead>
<tbody>
<!-- <tr></tr> -->
</tbody>
</table>

</output>
</body>
</html>
4 changes: 4 additions & 0 deletions js/_0_var.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const form = document.forms[0];
const allInputs = form.querySelectorAll("[type=date]");

const table = document.querySelector("table>tbody");
80 changes: 80 additions & 0 deletions js/_1_util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// convert Date() to date input value
function convertDate(date) {
let tmp = new Date(date);
let y = tmp.getFullYear();
let m = tmp.getMonth() + 1;
let d = tmp.getDate();
return `${y}-${m < 10 ? "0" + m : m}-${d < 10 ? "0" + d : d}`;
}


function ymd(result) {
let y = result.years;
let m = result.months;
let d = result.days;
return `${y}Y ${m < 10 ? "0" + m : m}M ${d < 10 ? "0" + d : d}D`;
}

function writeToTable(startDate, endDate, temporalDiff, fctemporalDiff, luxonDiff, third, isSame, bday) {
return (`
<tr>
<td>${startDate}</td>
<td>${endDate}</td>
<td>${temporalDiff}</td>
<td>${fctemporalDiff}</td>
<td>${luxonDiff}</td>
<td class="${third}">${third}</td>
<td>${isSame}</td>
<td>${bday}</td>
</tr>
`)
}

const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

// convert date to eg. "6 May 2020"
function convertDateToString(date) {
date = new Date(date);
let y = date.getFullYear();
let m = date.getMonth();
let d = date.getDate();
return `${d < 10 ? "0" + d : d} ${months[m]} ${y}`;
}


// Check is birthday
function checkBDay(startDate, endDate) {
let start = new Date(startDate);
let end = new Date(endDate);
let startDay = start.getDate();
let endDay = end.getDate();
let startMonth = start.getMonth();
let endMonth = end.getMonth();

return startMonth === endMonth && startDay === endDay ? "🎉" : "";
}


// new Date("2003-05-29") to new Date.UTC
function convertDateToUTC(date) {
let tmp = new Date(date);
let y = tmp.getFullYear();
let m = tmp.getMonth();
let d = tmp.getDate();
return {
y: y,
m: m,
d: d
}
}


// check same result
function checkSame(value1, value2) {
// remove - and whitespace
value1 = value1.replace(/\s/g, "");
value1 = value1.replace(/-/g, "");
value2 = value2.replace(/\s/g, "");
value2 = value2.replace(/-/g, "");
return value1 === value2 ? true : false;
}
64 changes: 64 additions & 0 deletions js/_2_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
start_input.value = "2022-01-31";
end_input.value = "2022-02-28";
document.onload = () => {
calcAge();
}


// local_date.textContent = "Your date format is " + new Date("2003-05-29").toLocaleDateString();
local_date.textContent = "📅 " + new Date("2003-05-29").toLocaleDateString();
local_date.onclick = () => { start_input.value = "2003-05-29"; end_input.valueAsDate = new Date(); calcAge() };

function oper(e, element) {
if (!element.value) { element.focus(); return }

let date = element.valueAsDate;
if (e.shiftKey) date.setDate(date.getDate() - 1);
else date.setDate(date.getDate() + 1);
element.valueAsDate = date;

calcAge();
}

start_oper.onclick = (e) => oper(e, start_input);
end_oper.onclick = (e) => oper(e, end_input);


function rcOper(e, element) {
e.preventDefault();
if (!element.value) { element.focus(); return }

let date = element.valueAsDate;
date.setDate(date.getDate() - 1);
element.valueAsDate = date;

calcAge();
}

start_oper.oncontextmenu = (e) => rcOper(e, start_input);
end_oper.oncontextmenu = (e) => rcOper(e, end_input);


trim.onclick = (e) => {
let excessRows = document.querySelectorAll('tbody > tr:nth-child(15) ~ tr');
if (e.shiftKey) excessRows = document.querySelectorAll('tbody > tr');;
excessRows.forEach(row => row.remove());
}
trim.oncontextmenu = (e) => {
e.preventDefault();
let rows = document.querySelectorAll('tbody > tr');
rows.forEach(row => row.remove());
}

swap.onclick = () => {
[start_input.valueAsDate, end_input.valueAsDate] = [end_input.valueAsDate, start_input.valueAsDate];
calcAge();
}


function preventSubmit(e) {
e.preventDefault();
calcAge();
}
form.addEventListener("submit", preventSubmit);

76 changes: 76 additions & 0 deletions js/_3_calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import fcTemporal from './js/lib/fctemporal.js';
// import luxon from "./js/lib/luxon.js";
// import ZitRos from "./js/lib/zit-ros.js";


function Temporal_fun(date1, date2) {
// date1 = convertDate(date1);
// date2 = convertDate(date2);
// let d1 = Temporal.PlainDate.from(date1).withCalendar('iso8601');
// let d2 = Temporal.PlainDate.from(date2).withCalendar('iso8601');
// return d1.until(d2, { largestUnit: 'year' });

return Temporal.Calendar.from('iso8601').dateUntil(
Temporal.PlainDate.from(date1),
Temporal.PlainDate.from(date2),
{ largestUnit: 'year' }
);
}

function fcTemporal_fun(date1, date2) {

return fcTemporal.Calendar.from('iso8601').dateUntil(
fcTemporal.PlainDate.from(date1),
fcTemporal.PlainDate.from(date2),
{ largestUnit: 'year' }
);
}

// function luxon_fun(date1, date2) {
// let end = luxon.DateTime.fromISO(date1);
// let start = luxon.DateTime.fromISO(date2);
// let diff = start.diff(end, ['years', 'months', 'days']);
// return diff.toObject();
// }

// function ZitRos_fun(date1, date2) {
// date1 = convertDateToUTC(date1);
// date2 = convertDateToUTC(date2);
// let diff = ZitRos(
// new Date(Date.UTC(date1.y, date1.m, date1.d, 0, 0, 0, 0)),
// new Date(Date.UTC(date2.y, date2.m, date2.d, 0, 0, 0, 0))
// );
// return diff;
// }

function calcAge() {
if (form.checkValidity()) {

let startDate = start_input.value;
let endDate = end_input.value;
// console.log(startDate);
// console.log(endDate);


const R1 = ymd(Temporal_fun(startDate, endDate));
const r2 = ymd(fcTemporal_fun(startDate, endDate));
const r3 = "" /* ymd(luxon_fun(startDate, endDate)) */;
const r4 = "" /* ymd(ZitRos_fun(startDate, endDate)) */;


// const isSame = checkSame(R1, r2) && checkSame(R1, r3) ? "✅" : checkSame(R1, r2) ? "☑" : "❌";
const isSame = checkSame(R1, r2) ? "✅" : "❌";
const isBDay = checkBDay(startDate, endDate);


startDate = convertDateToString(startDate);
endDate = convertDateToString(endDate);


let content = writeToTable(startDate, endDate, R1, r2, r3, r4, isSame, isBDay);
table.insertAdjacentHTML("afterbegin", content);

}
}

allInputs.forEach(input => input.addEventListener("input", calcAge));
Loading

0 comments on commit 7c1bcf7

Please sign in to comment.