Socket
Socket
Sign inDemoInstall

s-date

Package Overview
Dependencies
0
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    s-date

Tiny date/time formatter


Version published
Maintainers
1
Install size
87.4 kB
Created

Readme

Source

s-date

NPM version Dependencies build status NPM license

Same general purpose as moment.js, but in ~1kb minified.

This CommonJS module exports a single function that takes two parameters. The first is a format string, the second is an optional Date. If the date is omitted, it defaults to new Date();.

If you don't need full customizability for your date string, you probably don't need this library. Just use Date.toLocaleDateString() and related methods.

Usage

var date = require('s-date');

var myBirthday = new Date(1994, 10, 7);
date('{mm}/{dd}/{yyyy}', myBirthday); // '11/07/1994'

Formatting options

Example: For January 2, 2053 4:30pm + 45 seconds

String templateOutput exampleMeaning
{yyyy}20534-digit year
{yy}532-digit year
{mm}012-digit month
{m}1unpadded month
{Month}Januarymonth name
{Mo}Janmonth shortname
{d}2date
{ds}2nddate + suffix
{dd}022-digit date
{Weekday}Thursdayday of the week
{Day}Thuday shortname
{Dy}Thday shortername
{D}T1-char day name
{h24}16unpadded hours (24)
{hh24}16hours (24)
{hh}04hours (12)
{h}4unpadded hours (12)
{ampm}pmam/pm
{AMPM}PMAM/PM
{Minutes}30Minutes (padded)
{Seconds}45Seconds (padded)

To output the example above...

// Date(year, month-1, date, hours, minutes, seconds)
var someDay = new Date(2053, 0, 2, 4, 30, 45);
date('{Month} {d}, {yyyy} {h}:{Minutes}{ampm} + {Seconds} seconds', someDay);

Benchmarks

Run using benchmark.js on 2.3GHz Macbook Pro

s-date    x 428,936 ops/sec ±2.29% (79 runs sampled)
moment    x 271,963 ops/sec ±1.34% (85 runs sampled)
date-fns  x 196,827 ops/sec ±1.17% (80 runs sampled)
dayjs     x 121,425 ops/sec ±1.99% (87 runs sampled)
luxon     x 90,782 ops/sec ±1.13% (83 runs sampled)
Fastest is s-date

Keywords

FAQs

Last updated on 19 Jun 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc