GithubHelp home page GithubHelp logo

thdoan / strftime Goto Github PK

View Code? Open in Web Editor NEW
59.0 5.0 12.0 27 KB

A JavaScript port of strftime().

Home Page: https://thdoan.github.io/strftime/

License: MIT License

JavaScript 100.00%
javascript javascript-port strftime format-datetime date-formatting time-formatting

strftime's People

Contributors

prasadpilla avatar symbiatch avatar thdoan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

strftime's Issues

FEATURE REQUEST: support for abbreviated timezone

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.

Question: use of strftime in project

@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

Error converting due to wrong date type with instanceOf

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;
	}

Correction to %V calculation

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);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.