Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

date-formatter

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

date-formatter

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

date-formatter - date formatter by example; auto-builds the strftime format string from an example date

  • home :: github.com/feedreader/pluto
  • bugs :: github.com/feedreader/pluto/issues
  • gem :: rubygems.org/gems/date-formatter
  • rdoc :: rubydoc.info/gems/date-formatter
  • forum :: groups.google.com/group/wwwmake

Usage

The date by example lets you format dates e.g. "January 02, 2006" using an example as a format string e.g "January 02, 2006" instead of the classic strftime format specifier e.g. "%B %d, %Y". The date by example adds:

  • String#to_strfime
  • Date#format
  • Time#format
  • DateTime#format
  • NilClass#format

to the built-in classes.

The String#to_strftime Method

class String
  def to_strftime() DateByExample.to_strftime( self ); end
end

The new String#to_strftime method auto-builds the strftime format string from an example date:

require 'date/formatter'

p 'January 02, 2006'.to_strftime          #=> "%B %d, %Y"
p 'Mon, Jan 02'.to_strftime               #=> "%a, %b %d"
p '2 Jan 2006'.to_strftime                #=> "%-d %b %Y"
p 'Monday, January 2, 2006'.to_strftime   #=> "%A, %B %-d, %Y"

p 'Mon, Jan 02 3:00'.to_strftime          #=> "%a, %b %d %-H:%M"
p '2 Mon 2006 03:00'.to_strftime          #=> "%-d %b %Y %H:%M"

The Date#format Method

class Date
  def format( spec ) self.strftime( spec.to_strftime ); end
end

The new Date#format method formats the date like the passed in example:

date = Date.today   ## test run on 2020-02-09

p date.format( 'January 02, 2006' )         #=> "February 09, 2020"
p date.format( 'Mon, Jan 02' )              #=> "Sun, Feb 09"
p date.format( '2 Jan 2006' )               #=> "9 Feb 2020"
p date.format( 'Monday, January 2, 2006' )  #=> "Sunday, February 9, 2020"

The Time#format Method

class Time
  def format( spec ) self.strftime( spec.to_strftime ); end
end

The new Time#format method formats the time like the passed in example:

time = Time.now     ## test run on 2020-02-09 00:00

p time.format( 'January 02, 2006' )         #=> "February 09, 2020"
p time.format( 'Mon, Jan 02' )              #=> "Sun, Feb 09"
p time.format( '2 Jan 2006' )               #=> "9 Feb 2020"
p time.format( 'Monday, January 2, 2006' )  #=> "Sunday, February 9, 2020"

p time.format( 'Mon, Jan 02 3:00' )         #=> "Sun, Feb 09 0:00"
p time.format( '2 Mon 2006 03:00' )         #=> "9 Sun 2020 00:00"

The NilClass#format Method

class NilClass
  def format( spec ) ''; end
end

For convenience the new NilClass#format will catch format calls on nil
and NOT crash but return an empty string following the NilClass#to_s example:

p nil.format( 'January 02, 2006' )         #=> ""
p nil.to_s                                 #=> ""

License

The date-formatter scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Send them along to the wwwmake Forum/Mailing List. Thanks!

FAQs

Package last updated on 16 Feb 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc