Last active
August 27, 2018 20:33
-
-
Save Stas-Buzunko/36791f69a4753cfee1494b16dbef9128 to your computer and use it in GitHub Desktop.
date auto convert for chess.com articles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage | |
// 1) place an html element with id="time-holder" anywhere, like <span id="time-holder"></span> | |
// 2) place script below at the end of body tag | |
// 3) pass date in format 8/2/2018 7:00:00 PM UTC | |
// 4) pass language, keep empty for en | |
// 5) set whether to replace commas | |
// 6) in case you have mulptiple dates on the same page | |
// a) simply modify id in html from time-holder to time-holder-X but keep it unique | |
// b) copy paste this script and change in the last line of this script from time-holder to time-holder-X | |
// 7) in case it's title or visible part of article, and js won't be run, you can place any placeholder date | |
// into element with id="time-holder" so that it will be visible in preview | |
// const date = new Date('8/2/2018 7:00:00 PM UTC'); | |
// const language = [] // ['es'] or ['tr'] | |
// const shouldReplaceCommas = true // true / false changes Thursday, Aug 2, 10:00 PM to Thursday Aug 2, 10:00 PM | |
// if you need want to customize time, you can do this by modifying options params | |
// for example if you don't need month, then you can remove key-value pair ( they are separated by commas) | |
// "month":"long", | |
// if you don't need number of a month, then remove day key as well | |
// so it will look like | |
// const options = {"hour":"2-digit","minute":"2-digit","weekday":"long", 'timeZoneName': 'short'}; | |
<script type="text/javascript"> | |
(function() { | |
const date = new Date('8/2/2018 7:00:00 PM UTC'); | |
const language = ['en']; | |
const shouldReplaceCommas = false; | |
const options = {"month":"long","day":"numeric","hour":"2-digit","minute":"2-digit","weekday":"long", 'timeZoneName': 'short'}; | |
let formatted = date.toLocaleDateString(language, options); | |
if (shouldReplaceCommas) { | |
formatted = formatted.replace(',', ''); | |
} | |
document.getElementById('time-holder').innerHTML = formatted; | |
})() | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment