Last active
April 22, 2019 15:11
-
-
Save iamsainikhil/cf40c6555bc4f095b3339bac8242051d to your computer and use it in GitHub Desktop.
Formatting Dates// source https://jsbin.com/yobomin
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Formatting Dates</title> | |
</head> | |
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script> | |
<body> | |
<script id="jsbin-javascript"> | |
const sunrise = new Date('2019-02-23T11:49:59+00:00'); | |
const sunset = new Date('2019-02-23T22:55:28+00:00'); | |
// using moment.js | |
console.log(`Sunrise: ${moment(sunrise).format("HH:mm:ss A")} using moment.js`); | |
console.log(`Sunset: ${moment(sunset).format("HH:mm:ss A")} using moment.js`); | |
// using custom functions | |
function appendZero(n) { | |
return n < 10 ? `0${n}` : n; | |
} | |
function formatDate(date, single = true, format = 24, formatCapitalize = true) { | |
let h = date.getHours(); | |
let m = date.getMinutes(); | |
let s = date.getSeconds(); | |
const a = h >= 12 ? 'pm' : 'am'; | |
const A = a.toUpperCase(); | |
if (format === 12) { | |
h = h - 12; | |
} | |
const hh = appendZero(h); | |
const mm = appendZero(m); | |
const ss = appendZero(s); | |
const t = single ? `${h}:${m}:${s}` : `${hh}:${mm}:${ss}`; | |
return `${t} ${ formatCapitalize ? A : a } using custom built function` | |
} | |
console.log(`Sunrise: ${formatDate(sunrise, false)}`); | |
console.log(`Sunset: ${formatDate(sunset, false)}`); | |
console.log(`Sunset (12hr format): ${formatDate(sunset, false, 12)}`); | |
// 'https://api.sunrise-sunset.org/json?lat=38.9318&lng=-77.3527&date=2019-02-23' | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const sunrise = new Date('2019-02-23T11:49:59+00:00'); | |
const sunset = new Date('2019-02-23T22:55:28+00:00'); | |
// using moment.js | |
console.log(`Sunrise: ${moment(sunrise).format("HH:mm:ss A")} using moment.js`); | |
console.log(`Sunset: ${moment(sunset).format("HH:mm:ss A")} using moment.js`); | |
// using custom functions | |
function appendZero(n) { | |
return n < 10 ? `0${n}` : n; | |
} | |
function formatDate(date, single = true, format = 24, formatCapitalize = true) { | |
let h = date.getHours(); | |
let m = date.getMinutes(); | |
let s = date.getSeconds(); | |
const a = h >= 12 ? 'pm' : 'am'; | |
const A = a.toUpperCase(); | |
if (format === 12) { | |
h = h - 12; | |
} | |
const hh = appendZero(h); | |
const mm = appendZero(m); | |
const ss = appendZero(s); | |
const t = single ? `${h}:${m}:${s}` : `${hh}:${mm}:${ss}`; | |
return `${t} ${ formatCapitalize ? A : a } using custom built function` | |
} | |
console.log(`Sunrise: ${formatDate(sunrise, false)}`); | |
console.log(`Sunset: ${formatDate(sunset, false)}`); | |
console.log(`Sunset (12hr format): ${formatDate(sunset, false, 12)}`); | |
// 'https://api.sunrise-sunset.org/json?lat=38.9318&lng=-77.3527&date=2019-02-23' | |
</script></body> | |
</html> |
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
const sunrise = new Date('2019-02-23T11:49:59+00:00'); | |
const sunset = new Date('2019-02-23T22:55:28+00:00'); | |
// using moment.js | |
console.log(`Sunrise: ${moment(sunrise).format("HH:mm:ss A")} using moment.js`); | |
console.log(`Sunset: ${moment(sunset).format("HH:mm:ss A")} using moment.js`); | |
// using custom functions | |
function appendZero(n) { | |
return n < 10 ? `0${n}` : n; | |
} | |
function formatDate(date, single = true, format = 24, formatCapitalize = true) { | |
let h = date.getHours(); | |
let m = date.getMinutes(); | |
let s = date.getSeconds(); | |
const a = h >= 12 ? 'pm' : 'am'; | |
const A = a.toUpperCase(); | |
if (format === 12) { | |
h = h - 12; | |
} | |
const hh = appendZero(h); | |
const mm = appendZero(m); | |
const ss = appendZero(s); | |
const t = single ? `${h}:${m}:${s}` : `${hh}:${mm}:${ss}`; | |
return `${t} ${ formatCapitalize ? A : a } using custom built function` | |
} | |
console.log(`Sunrise: ${formatDate(sunrise, false)}`); | |
console.log(`Sunset: ${formatDate(sunset, false)}`); | |
console.log(`Sunset (12hr format): ${formatDate(sunset, false, 12)}`); | |
// 'https://api.sunrise-sunset.org/json?lat=38.9318&lng=-77.3527&date=2019-02-23' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment