the_time.js

1.0.0 • Public • Published

TheTime.js

Date object wrapper for Node.js and Browser JS

Makes work with JS Date object easier and more humane

Download TheTime.js

Keywords

JavaScript Date wrapper, JavaScript Date Object, JavaScript Date and Time Functions, JavaScript Date Comparison

Use with Node.js

require('/path/to/the_time.js');

console.log(Moment.o_mori)

Use with Browser

<head>
<script src="/path/to/the_time.js" type="text/javascript"></script>
</head>
console.log(Moment.o_mori)

Description

Easy way define Date object with Wrapper

  • with Array of numbers
  • with Hash of numbers
  • with String

Easy way to convert Date to Wrapper and Wrapper to Date

  • (new Date).to_m() and (new Moment).date

Months

  • stop working with months with machine style - from 0 to 11
  • start working with months with human style - from 1 to 12
  • forget about Month +1/-1 correction for Date object

Days

  • stop working with day number with machine style - from 0 to 6
  • start working with day number with human style - from 1 to 7
  • SORRY, but it using Russian meaning of days numbers (1 - Monday, 7 - Sunday)

Unix time

  • to forget about Unix time seconds *1000 and /1000 correction (because it is not secs, but ms)

Calendar function

  • I want to have some functions for ease calendar building

Easy way to access to date variables

  • usable setters and getters (with Ruby style)

Easy shift of date

  • usable shift methods

Easy way to get difference of dates, and date comparison

  • usable difference method and set of comparison methods

Easy way to create Localization for any Langs

  • En and Ru in preset

And more...

Moment API

Moment object initialization

Date to Moment convertion

date = new Date
M = date.to_m()

M.date => current Date

new Moment

M = new Moment
M.date => current Date

with array of numbers

[YEAR, MONTH, DAY, HOURS, MINS, SECS, MS, SHIFT]
M = new Moment([2007, 10, 23, 13, 10, 23])

M.date => Date {Tue Oct 23 2007 13:10:23 GMT+0400}

with hash of numbers

{ year: YEAR, month: MONTH, day: DAY, hours: HOURS, mins: MINS, secs: SECS, ms: MS, shift: SHIFT }
M = new Moment({ year: 2010, month: 3, day: 12, hours: 14, mins: 52, secs: 13})

M.date => Date {Fri Mar 12 2010 14:52:13 GMT+0400}

with Date object

xdate = new Date
M = new Moment(xdate)

M.date => Date {Thu Jun 21 2012 07:36:06 GMT+0400}

with String

xdate = new Date
str = xdate.toString()
M = new Moment(str)

M.date => Date {Thu Jun 15 2012 08:11:48 GMT+0400}

Moment API getters

M = new Moment
M.date    => Date object
M.year
M.month
M.day
M.hours
M.mins
M.secs
M.ms
M.shift   => GMT shift (Timezone Offset)
M.unix    => seconds from Unix epoch
M.unix_ms => milliseconds from Unix

Moment API setters

M = new Moment
M.set(Date | [] | {} | String)
M.set_year(2006)
M.set_month(5)
M.set_day(25)
M.set_hours(23)
M.set_mins(59)
M.set_secs(12)
M.set_ms(126)
M.set_unix(1234567890)           => M.gmt() => Fri, 13 Feb 2009 23:31:30 GMT
M.set_unix_ms(1234567890 * 1000) => M.gmt() => Fri, 13 Feb 2009 23:31:30 GMT

Moment API methods

M = new Moment
M.to_i()
M.to_s()
M.utc()
M.gmt()
M.to_a()
M.to_hash()
M.dump()
M.to_gmt_a()
M.to_gmt_hash()
  • M.to_i() - alias of M.unix
  • M.to_s() - M.date.toString()
  • M.utc() - M.date.toUTCString()
  • M.gmt() - M.date.toGMTString()
  • M.to_a() - Array of Moment's instances
  • M.to_hash() - Hash (Object) of Moment's instances
  • M.dump() - Moment's instances DUMP string
  • M.to_gmt_a() - Array of Moment's instances with GMT
  • M.to_gmt_hash() - Hash (Object) of Moment's instances with GMT

Moment API helpers-methods for Calendars Building

M = new Moment
M.today([] | String)
M.get_month_length()
M.get_day_of_week()
M.first_day_of_month()
M.last_day_of_month()
M.days_before_month()
M.days_after_month()
  • M.today() - today? [year, month, day] or Moment
  • M.get_month_length() - count of M.month's days
  • M.get_day_of_week() - M.day's position in week (Monday - first [1], Sunday - last [7])
  • M.first_day_of_month() - M.month's first day position in week (Monday - first [1], Sunday - last [7])
  • M.last_day_of_month() - M.month's last day position in week (Monday - first [1], Sunday - last [7])
  • M.days_before_month() - empty day cells at begin of M.month Range[0-6]
  • M.days_after_month() - empty day cells at end of M.month Range[0-~14]

Moment API Shift methods. Returns Array[YEAR, MONTH]

M = new Moment
M.shift_years(INT)  => [Year, Month]
M.shift_months(INT) => [Year, Month]

Moment API Shift methods. Shift by Unix time shifting

M = new Moment
M.shift_weeks(5)
M.shift_days(7)
M.shift_hours(1)
M.shift_mins(25)
M.shift_secs(13)

Moment API Comparsions methods

M1 = new Moment
M2 = new Moment

M1.less(M2)
M1.less_or_equal(M2)
M1.equal(M2)
M1.greater_or_equal(M2)
M1.greater(M2)

Moment API Comparsions shortcut methods

M1 = new Moment
M2 = new Moment

M1.l(M2)
M1.loe(M2)
M1.e(M2)
M1.goe(M2)
M1.g(M2)

Moment API. Day of week (bool functions)

M = new Moment

M.is_weekend()

M.is_monday()
M.is_tuesday()
M.is_wednesday()
M.is_thursday()
M.is_friday()
M.is_saturday()
M.is_sunday()

M.is_mon()
M.is_tue()
M.is_wed()
M.is_thu()
M.is_fri()
M.is_sat()
M.is_sun()

Moment API. Part of day

M = new Moment

M.is_morning()
M.is_day()
M.is_evening()
M.is_night()

M.part_of_day() => 'morning' | 'day' | 'evening' | 'night'

Moment API. Extended

M = new Moment

X = Moment.want(M) => if M is Moment then return M else parse M and create new Moment

M.start_of_day()
M.end_of_day()

M.start_of_week()
M.end_of_week()

M.start_of_month()
M.end_of_month()

M.start_of_year()
M.end_of_year()

M.day_name()
M.month_name()

Moment Diff API

M1 = new Moment
M2 = new Moment

M1.diff(M2)      => Object with different diff instances

Moment Localization

// Add some locales
Moment.add_locale DefaultMomentLocale.locale_name, DefaultMomentLocale.locale
Moment.add_locale RuMomentLocale.locale_name,      RuMomentLocale.locale

// Use locale by default
Moment.use_locale 'ru'

M = new Moment

// Use locale for this instances
M.use_current_locale('ru')
M.day_name() => 'Среда'

M.use_current_locale('en')
M.day_name() => 'Wednesday'

Moment Diff object structure

M1   = new Moment([2010, 10, 26])
M2   = new Moment([2012, 3,  11])
diff = M1.diff(M2)

diff.days       => -502
diff.days_abs   => 502
  
diff.hours      => -12048
diff.hours_abs  =>  12048
  
diff.mins       => -722880
diff.mins_abs   => 722880
  
diff.months     => -17
diff.months_abs => 17
  
diff.ms         => -43372800000
diff.ms_abs     => 43372800000
  
diff.secs       => -43372800
diff.secs_abs   => 43372800
  
diff.shift      => 0
diff.unix       => -43372800
  
diff.unix_ms    => -43372800000

diff.weeks      => -71.71428571428571  
diff.weeks_abs  => 71.71428571428571
  
diff.years     => -2
diff.years_abs => 2

License MIT

Copyright (c) 2012 Ilya N. Zykin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Node.js env install

Base

sudo apt-get install python-software-properties

sudo apt-add-repository ppa:chris-lea/node.js

sudo apt-get update

Install Node.js

sudo apt-get install nodejs

Install NPM

wget http://npmjs.org/install.sh

chmod 777 ./install.sh

sudo ./install.sh

Install coffee

sudo npm install coffee-script -g # globally

sudo npm install coffee-script # locally

CoffeeScript compile

coffee -c -o ./js/ ./coffee/

The --watch feature depends on Node v0.6.0+

./node_modules/coffee-script/bin/coffee -cw -o ./js/ ./coffee/

Compile without security closure

./node_modules/coffee-scriptin/coffee -cwb -o ./js/ ./coffee/

Test

node test.js

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i the_time.js

    Weekly Downloads

    0

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • the-teacher