Skip to content

Instantly share code, notes, and snippets.

View sayyedhammadali's full-sized avatar

Sayyed Hammad Ali sayyedhammadali

View GitHub Profile
@sayyedhammadali
sayyedhammadali / copy-to-clipboard.txt
Last active October 16, 2021 12:35
Copy to Clipboard
const copyToClipboard = (text) => navigator.clipboard.writeText(text);
copyToClipboard("This Sring is Copied To Clipboard.");
@sayyedhammadali
sayyedhammadali / rgb-to-hex.txt
Created October 2, 2021 21:46
Convert RGB to Hex
const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
rgbToHex(0, 51, 255);
// Result: #0033ff
@sayyedhammadali
sayyedhammadali / day-dif.txt
Created October 2, 2021 21:52
Find the number of days between two dataes
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2020-10-21"), new Date("2021-10-22"))
// Result: 366
@sayyedhammadali
sayyedhammadali / generate-random-hex.txt
Created October 2, 2021 21:56
Generate Random Hex
const randomHex = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0")}`;
console.log(randomHex());
// Result: #92b008
@sayyedhammadali
sayyedhammadali / is-week-day.txt
Last active October 2, 2021 22:03
Check if the provided day is a weekday
const isWeekday = (date) => date.getDay() % 6 !== 0;
console.log(isWeekday(new Date(2021, 0, 11)));
// Result: true (Monday)
console.log(isWeekday(new Date(2021, 0, 10)));
// Result: false (Sunday)
@sayyedhammadali
sayyedhammadali / fahrenheit-celsius.txt
Created October 2, 2021 22:07
Convert Fahrenheit / Celsius
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
// Examples
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4
fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0
@sayyedhammadali
sayyedhammadali / is-apple-device.txt
Created October 2, 2021 22:11
Check if the current user is on an Apple device
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
console.log(isAppleDevice);
// Result: will return true if user is on an Apple device
@sayyedhammadali
sayyedhammadali / time-from-date.txt
Created October 2, 2021 22:13
Get the time from a date
const timeFromDate = date => date.toTimeString().slice(0, 8);
console.log(timeFromDate(new Date(2021, 0, 10, 17, 30, 0)));
// Result: "17:30:00"
console.log(timeFromDate(new Date()));
// Result: will log the current time
@sayyedhammadali
sayyedhammadali / strip-html.txt
Last active October 5, 2021 09:12
Strip HTML From Text
const stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';
stripHtml('<h1>Hello <strong>World</strong>!!!</h1>');
// Result: Hello World!!!
@sayyedhammadali
sayyedhammadali / toggle-element-display.txt
Last active October 5, 2021 09:50
Toggle Display of an Element
const toggleElementDisplay = element => element.style.display = (element.style.display === "none" ? "block" : "none");
toggleElementDisplay(document.body)
// Result: Page body should be invisible, if it was present before and on executing command again it should be toggled