Convert Figma logo to code with AI

lukeed logotinydate

A tiny (349B) reusable date formatter. Extremely fast!

1,065
16
1,065
1

Top Related Projects

34,434

⏳ Modern JavaScript date utility library ⌛️

47,968

Parse, validate, manipulate, and display dates in javascript.

46,822

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

A node.js package for Steven Levithan's excellent dateFormat() function.

Quick Overview

Tinydate is a lightweight JavaScript library for formatting dates. It provides a simple and efficient way to create custom date formatting functions with minimal overhead, making it ideal for projects where performance and size are critical.

Pros

  • Extremely small size (340 bytes minified + gzipped)
  • Fast performance due to pre-compilation of format strings
  • No dependencies
  • Works in both browser and Node.js environments

Cons

  • Limited functionality compared to more comprehensive date libraries
  • Doesn't support parsing dates or complex date manipulations
  • No built-in localization support
  • Lacks some advanced formatting options available in other libraries

Code Examples

Basic date formatting:

import tinydate from 'tinydate';

const stamp = tinydate('{YYYY}-{MM}-{DD}');
console.log(stamp(new Date())); // Output: 2023-05-10

Custom formatting with time:

const timeStamp = tinydate('{HH}:{mm}:{ss}');
console.log(timeStamp(new Date())); // Output: 14:30:45

Using custom delimiters:

const customStamp = tinydate('{YYYY}/{MM}/{DD} at {HH}:{mm}');
console.log(customStamp(new Date())); // Output: 2023/05/10 at 14:30

Getting Started

To use Tinydate in your project, follow these steps:

  1. Install the package:

    npm install tinydate
    
  2. Import and use in your JavaScript file:

    import tinydate from 'tinydate';
    
    const formatter = tinydate('{YYYY}-{MM}-{DD}');
    console.log(formatter(new Date()));
    

That's it! You can now create custom date formatters using Tinydate's simple syntax.

Competitor Comparisons

34,434

⏳ Modern JavaScript date utility library ⌛️

Pros of date-fns

  • Comprehensive set of date manipulation functions
  • Modular architecture allowing for tree-shaking
  • Extensive browser and environment support

Cons of date-fns

  • Larger bundle size due to its extensive feature set
  • Steeper learning curve with more functions to understand
  • Potentially overkill for simple date formatting tasks

Code Comparison

date-fns:

import { format } from 'date-fns'

const formattedDate = format(new Date(), 'yyyy-MM-dd HH:mm:ss')
console.log(formattedDate) // Output: 2023-05-10 14:30:45

tinydate:

import tinydate from 'tinydate'

const stamp = tinydate('{YYYY}-{MM}-{DD} {HH}:{mm}:{ss}')
console.log(stamp(new Date())) // Output: 2023-05-10 14:30:45

Summary

date-fns offers a comprehensive suite of date manipulation functions with excellent browser support and a modular architecture. However, it comes with a larger bundle size and a steeper learning curve. tinydate, on the other hand, provides a lightweight solution for simple date formatting tasks but lacks the extensive functionality of date-fns. The choice between the two depends on the specific needs of your project, with date-fns being more suitable for complex date operations and tinydate for basic formatting requirements.

47,968

Parse, validate, manipulate, and display dates in javascript.

Pros of Moment

  • Extensive feature set for parsing, validating, manipulating, and formatting dates
  • Robust internationalization support with locales for many languages
  • Well-established library with a large community and extensive documentation

Cons of Moment

  • Large bundle size, which can impact performance in web applications
  • No longer recommended for new projects due to its legacy status
  • More complex API compared to lightweight alternatives

Code Comparison

Moment:

import moment from 'moment';

const formattedDate = moment().format('YYYY-MM-DD HH:mm:ss');
const futureDate = moment().add(1, 'week').calendar();

Tinydate:

import tinydate from 'tinydate';

const stamp = tinydate('{YYYY}-{MM}-{DD} {HH}:{mm}:{ss}');
const formattedDate = stamp(new Date());

Tinydate is a minimalist date formatting library, focusing solely on formatting dates with a small footprint. It offers a simple API and tiny bundle size, making it ideal for projects where only basic date formatting is needed. However, it lacks the extensive features and manipulation capabilities of Moment.

Moment, while feature-rich and widely adopted, is no longer recommended for new projects due to its large size and maintenance status. For modern applications, consider using lighter alternatives like Tinydate for simple formatting tasks, or more comprehensive solutions like date-fns or Luxon for advanced date operations.

46,822

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

Pros of dayjs

  • More comprehensive feature set, including parsing, manipulation, and formatting
  • Supports localization and internationalization out of the box
  • Offers a plugin system for extending functionality

Cons of dayjs

  • Larger bundle size compared to tinydate
  • May have more overhead for simple date formatting tasks

Code Comparison

tinydate:

import tinydate from 'tinydate';
const stamp = tinydate('{YYYY}-{MM}-{DD}');
console.log(stamp(new Date()));

dayjs:

import dayjs from 'dayjs';
const date = dayjs();
console.log(date.format('YYYY-MM-DD'));

Summary

dayjs offers a more feature-rich solution for date and time manipulation, with support for localization and plugins. It's suitable for projects requiring extensive date operations. However, this comes at the cost of a larger bundle size.

tinydate, on the other hand, is a lightweight alternative focused solely on date formatting. It's ideal for projects where minimal date functionality is needed and bundle size is a concern.

The choice between the two depends on the specific requirements of your project, balancing between functionality and performance considerations.

A node.js package for Steven Levithan's excellent dateFormat() function.

Pros of node-dateformat

  • More comprehensive date formatting options
  • Supports localization and custom locale settings
  • Allows parsing of date strings into Date objects

Cons of node-dateformat

  • Larger package size (about 16KB minified)
  • More complex API, potentially steeper learning curve
  • Slower performance due to more features and flexibility

Code Comparison

node-dateformat:

var dateFormat = require('dateformat');
var now = new Date();
console.log(dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"));
console.log(dateFormat(now, "isoDateTime"));

tinydate:

import tinydate from 'tinydate';
const stamp = tinydate('{YYYY}-{MM}-{DD} {HH}:{mm}:{ss}');
console.log(stamp(new Date()));

Key Differences

  • node-dateformat offers more built-in format options and flexibility
  • tinydate focuses on simplicity and performance, with a smaller API surface
  • node-dateformat supports parsing date strings, while tinydate is primarily for formatting
  • tinydate has a significantly smaller package size (about 300 bytes minified and gzipped)
  • tinydate uses a simpler template string approach for defining date formats

Both libraries serve different needs: node-dateformat is better suited for complex date formatting requirements, while tinydate excels in lightweight, performance-critical scenarios where basic date formatting is sufficient.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

tinydate CI licenses downloads

A tiny (349B) reusable date formatter. Extremely fast!

Demo

Inspired by tinytime, this module returns a "render" function that efficiently re-render your deconstructed template. This allows for incredibly performant results!

However, please notice that this only provides a limited subset of Date methods.
If you need more, tinytime or date-fns are great alternatives!

Install

$ npm install --save tinydate

Usage

const tinydate = require('tinydate');
const fooDate = new Date('5/1/2017, 4:30:09 PM');

const stamp = tinydate('Current time: [{HH}:{mm}:{ss}]');

stamp(fooDate);
//=> Current time: [16:30:09]

stamp();
//=> Current time: [17:09:34]

API

tinydate(pattern, dict?)(date?)

Returns: Function

Returns a rendering function that will optionally accept a date value as its only argument.

pattern

Type: String
Required: true

The template pattern to be parsed.

dict

Type: Object
Required: false

A custom dictionary of template patterns. You may override existing patterns or declare new ones.

Important: All dictionary items must be a function and must control its own formatting.
For example, when defining your own {ss} template, tinydate will not pad its value to two digits.

const today = new Date('2019-07-04, 5:30:00 PM');

// Example custom dictionary:
//   - Adds {MMMM}
//   - Overrides {DD}
const stamp = tinydate('Today is: {MMMM} {DD}, {YYYY}', {
	MMMM: d => d.toLocaleString('default', { month: 'long' }),
	DD: d => d.getDate()
});

stamp(today);
//=> 'Today is: July 4, 2019'

date

Type: Date
Default: new Date()

The date from which to retrieve values. Defaults to current datetime if no value is provided.

Patterns

  • {YYYY}: full year; eg: 2017
  • {YY}: short year; eg: 17
  • {MM}: month; eg: 04
  • {DD}: day; eg: 01
  • {HH}: hours; eg: 06 (24h)
  • {mm}: minutes; eg: 59
  • {ss}: seconds; eg: 09
  • {fff}: milliseconds; eg: 555

Benchmarks

# Node v10.13.0

tinydate    x 160,834,214 ops/sec ±0.21% (96 runs sampled)
tinytime    x  44,602,162 ops/sec ±0.34% (97 runs sampled)
time-stamp  x     888,153 ops/sec ±1.27% (86 runs sampled)

License

MIT © Luke Edwards

NPM DownloadsLast 30 Days