thdoan / strftime Goto Github PK
View Code? Open in Web Editor NEWA JavaScript port of strftime().
Home Page: https://thdoan.github.io/strftime/
License: MIT License
A JavaScript port of strftime().
Home Page: https://thdoan.github.io/strftime/
License: MIT License
Love this library! I use it all the time in preference to what I consider the incomprehensible and poorly-documented Javascript Date formatters. But the one thing I can't do is get the abbreviate timezone, which I basically always want. The strftime
%Z
option supposedly returns "the timezone name or abbreviation," which are two very different strings -- e.g. "Eastern Daylight Time"
vs "EDT"
. In practice, it always seems to be the longer version, but either way, since I can't rely on the output, the %Z
is worthless to me (and I would imagine the majority of web developers).
The same issue of course occurs in the Unix strftime
-- the problem doesn't originate with this library.
So I end up writing my own ugly wrappers around strftime
to stitch this in everywhere:
export function shortTz(date?: Date | null): string | undefined {
if (!date) { return undefined }
const timeString = date.toLocaleTimeString('en', {timeZoneName: 'short'})
return timeString.match(' ([A-Z+0-9]+)$')![1]!
}
export function formatTimeWithShortTz(format: string, time: Date): string {
return `${stftime(format, time)} ${shortTz(time)}`
}
Though strftime
is intended to be a port of the Unix version, this one simple enhancement to me vastly increases its usefulness. Would it be possible to extend it to cover this case, with the understanding that abbreviations aren't standardized and may be browser-dependent? Since %Z
is non-deterministic anyway, changing it return the abbreviated time zone (which seems more useful and common than the spelled-out one) would not break backwards compatibility. Ideally we could add a second %something
for the spelled-out case and cover both.
console.log(strftime("%Y_%m_%d_%H_%M_%S_", Date.now()));
@thdoan
Hello !
I came across your library while working on replacement for a deprecated Firefox function toLocaleFormat()
Your library is excellent and tiny!
Any problem using for a Thunderbird add-on? I will of course give attribution in the contributor section of the manifest. I think the license mixing with the Mozilla license should be okay.
Cheers and thanks in advance.
@cleidigh
Before checking for instanceof, date needs to be converted to Date object
and need to be checked for valid date just incase date is a null or empty string
function strftime(sFormat, sDate) {
if (typeof sFormat !== 'string') {
return '';
}
if (sDate == "") {return sDate;}
let date = new Date(sDate);
if (!(date instanceof Date && !isNaN(date))) {
return sDate;
}
I think line 92 should read:
return zeroPad(1 + Math.ceil((n1stThu - target.valueOf()) / 604800000), 2);
rather than:
return zeroPad(1 + Math.ceil((n1stThu - target) / 604800000), 2);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.