Coder Social home page Coder Social logo

date-utils's Introduction

Date-Utils: Polyfills for the Date object

NOTE

Version 2 Feature and Changes discussion is being held at #37

In a nutshell

  • Micro-Framework adding missing functionality to the Date object
  • Useable as a polyfill in the browser
  • Useable as a polyfill in Node.js
  • Works in CouchDB

Using within a Browser

<script type="text/javascript" src="date-utils.min.js"></script>

Using with Node.js

$ npm install date-utils

require('date-utils');

Note: This did not work in the REPL before Node.js 0.6 due to how Node.js handles context in the REPL.

Changing Languages

require('date-utils').language("es")

Supported languages:

  • Spanish - "es"
  • French - "fr"
  • Portuguese Brazilian - "pt-BR"

Documentation

Documentation (nearing completion) can be found at https://jerrysievert.github.io/date-utils

Development

Make sure you have gulp installed (npm install gulp -g). After that hit npm install to get the dependencies. Finally, hit gulp. This will build the library initially. If you make changes to the library, it will compile the minified version automatically.

Testing

$ npm test

Complexity and Minification

$ gulp

date-utils's People

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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

date-utils's Issues

Add `remove` to README

Hi,

I noticed you added remove. It might be nice to mention that at README too as people like to use NPM as API doc and might consequently miss it.

bad getOrdinalNumber()

var d = new Date();
    console.log (d);
    console.log (d.getOrdinalNumber());
var d2 = new Date('01-01-2011 GMT');
    console.log (d2);
    console.log (d2.getOrdinalNumber());
var d3 = new Date('12-31-2010 GMT');
    console.log (d3);
    console.log (d3.getOrdinalNumber());

output

Thu, 28 Jul 2011 13:12:32 GMT
210
Sat, 01 Jan 2011 00:00:00 GMT
2
Fri, 31 Dec 2010 00:00:00 GMT
366

should be :

Thu, 28 Jul 2011 13:12:32 GMT
209
Sat, 01 Jan 2011 00:00:00 GMT
0 
Fri, 31 Dec 2010 00:00:00 GMT
365

I'm currently rewriting from perl something in node, and FYI, the day of the year is expressed in a 0..364(+1) range

can't get to work with node 0.5.4

require("./date-utils")
{}
Date.today()
TypeError: Object function Date() { [native code] } has no method 'today'
at [object Context]:1:6
at Interface. (repl.js:168:22)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream. (readline.js:73:12)
at ReadStream.emit (events.js:88:20)
at ReadStream._emitKey (tty_posix.js:306:10)
at ReadStream.onData (tty_posix.js:69:12)

Number of Days between

Lets say we have March 12th as a date and March 20th. Technically the number of days between these two days is 7, as the library returns. In my mind however it seems like it should be 8 days. For instance lets say I was using this to pay bills. I want to tell my clients I will be paying their bills on the 20th which to them is "8 days from now", however the library returns 7. Should I just do

var daysBetween = d1.getDaysBetween(d2) + 1

To simulate this experience?

Cannot find module 'date-utils'

when running a node.js script require 'date-utils' module , it return error:

➜  mobileapi git:(master) ✗ node server.js                       
module.js:471
    throw err;
    ^

Error: Cannot find module 'date-utils'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/loginuser/workspace/node/node/mobileapi/server.js:3:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
➜  mobileapi git:(master) ✗ 

Problem about Check for leap year

Hi:
I learn programming only a few time.
When I use Date.pase() to check if a datestring is valid. I found it can't check leap year correctly . So I view some content of the date-utils.js file. I find there are some places with the code like (month === n) where the month is a string and n a number. I wonder if their type need to be converted the same. So I ask some help.
Please forgive me for my bad english.
Thanks!

How do you feel about... ?

Jerry,

How do you feel about extending other object's and their prototypes? Specifically, I would love to have:

1.month.from_now.getMonthName();
// or
1.month.from_now.MonthName();
// since you can't really set the month name

How do you feel about that? I think it would be a good approach to help with the #addDays issues I've seen added in the past few months.

Thoughts?

Property 'jui' redefine problem

I'm using date-utils in my react native project. After I upgrade the date-utils to the latest version, my project is keeping crash. From the log info, I got a message:

Attempted to redefine property 'jui'

Then I located the problem in date-utils, the code:

https://github.com/JerrySievert/date-utils/blob/master/lib/date-utils.js#L1722

monthNames = {
    'jan': 0,
    'janvier': 0,
    'fév': 1,
    'février': 1,
    'mar': 2,
    'mars': 2,
    'avr': 3,
    'avril': 3,
    'mai': 4,
    'jui': 5,
    'juin': 5,
    'jui': 6,
    'juillet': 6,
    'aoû': 7,
    'août': 7,
    'sep': 8,
    'septembre': 8,
    'oct': 9,
    'octobre': 9,
    'nov': 10,
    'novembre': 10,
    'déc': 11,
    'décembre': 11
};

The property jui is redefined. Is any better way to realize this month names define?

This problem appears after v1.2.16.

error in the parsing of the weekday

Date.parse('July 2014 Th 24', 'MMM y E d'); // works
Date.parse('July 2014 Thu 24', 'MMM y E d'); // fails

The 'u' symbol is treated as trailing character after recognized name 'th'.
The solution is simple: iterate the allDys in reverse order.

d.add({week: 1}) not implemented

addWeeks:

    polyfill('addWeeks', function (weeks) {
        return this.add({ days: (weeks * 7) });
    });

no weeks here:

    polyfill('add', function (obj) {
        if (obj.milliseconds !== undefined) {
            this.setMilliseconds(this.getMilliseconds() + obj.milliseconds);
        }
        if (obj.seconds !== undefined) {
            this.setSeconds(this.getSeconds() + obj.seconds);
        }
        if (obj.minutes !== undefined) {
            this.setMinutes(this.getMinutes() + obj.minutes);
        }
        if (obj.hours !== undefined) {
            this.setHours(this.getHours() + obj.hours);
        }
        if (obj.days !== undefined) {
            this.setDate(this.getDate() + obj.days);
        }
        if (obj.months !== undefined) {
            this.setMonth(this.getMonth() + obj.months);
        }
        if (obj.years !== undefined) {
            this.setFullYear(this.getFullYear() + obj.years);
        }
        return this;
    });

Function add/addMonths compute error

new Date().addMonths(0)
Fri May 31 2019 17:07:50 GMT+0800 (China Standard Time)
new Date().addMonths(1)
Mon Jul 01 2019 17:07:58 GMT+0800 (China Standard Time)
new Date().addMonths(2)
Wed Jul 31 2019 17:08:01 GMT+0800 (China Standard Time)
new Date().addMonths(3)
Sat Aug 31 2019 17:08:03 GMT+0800 (China Standard Time)
new Date().add({ months: 0 })
Fri May 31 2019 17:08:24 GMT+0800 (China Standard Time)
new Date().add({ months: 1 })
Mon Jul 01 2019 17:08:27 GMT+0800 (China Standard Time)
new Date().add({ months: 2 })
Wed Jul 31 2019 17:08:29 GMT+0800 (China Standard Time)
new Date().add({ months: 3 })
Sat Aug 31 2019 17:08:32 GMT+0800 (China Standard Time)

no method errors

You may know about this, but I can't get the examples in the readme to work on node 0.4:

> require('date-utils');
{}
> var date = new Date(Date.parse('2011-01-09T17:57:17.231Z'));
> var abbreviated_month = date.getMonthAbbr();
TypeError: Object Sun Jan 09 2011 12:57:17 GMT-0500 (EST) has no method 'getMonthAbbr'
    at [object Context]:1:30
    at Interface.<anonymous> (repl.js:171:22)
    at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)
    at ReadStream._emitKey (tty_posix.js:307:10)
    at ReadStream.onData (tty_posix.js:70:12)

subtract instance methods

Unless I'm missing something I think we're missing a whole host of functionality. Basically all of the add method's opposites:

d.add({ milliseconds: 30,
minutes: 1,
hours: 4,
seconds: 30,
days: 2,
weeks: 1,
months: 3,
years: 2}); // adds time to existing time

d.addMilliseconds(number); // add milliseconds to existing time
d.addSeconds(number); // add seconds to existing time
d.addMinutes(number); // add minutes to existing time
d.addHours(number); // add hours to existing time
d.addDays(number); // add days to existing time
d.addWeeks(number); // add weeks to existing time
d.addMonths(number); // add months to existing time
d.addYears(number); // add years to existing time

d.subtract Would be nice? or d.ago ?

problems with addHours

hello,
i have try to add "addHours" to the current time, but is not working right, this is my example

if the current time is 00.01 AM 15 December 2012 and i add 10 hours, the resulting time must be:
10.01 AM 16 December 2012, but the "addHours" returns 10.01 AM 15 December 2012.

i guess that this behavior is present in all the add methods, is possibile to investigate the problem?

toYMD returning UTC

polyfill('toYMD', function (separator) {
        separator = typeof separator === 'undefined' ? '-' : separator;
        return this.getUTCFullYear() + separator + pad(this.getUTCMonth() + 1, 2) +
            separator + pad(this.getUTCDate(), 2);
    });

Is the above really accurate?

getMonthsBetween inconsitency

I'm confused to the fact why this returns two separate results.

> var now = new Date()
> var twoMonths = new Date().addMonths(2)
> now.getMonthsBetween(twoMonths)
2.033333333333333
> twoMonths.getMonthsBetween(now)
1.0333299999999999

I assume they'd be the same with a result of 2.

"addDays" doesn't flow to the next/previous month

It looks like "addDays" doesn't go to previous/next month at a month boundary as one might expect. It would be nice if you could mention about this at the documentation or change the behavior. :)

No bower versions

Doesn't look like there are any git tags on this repo, so you can't specify versions in Bower. Mind adding a tag at least for this current version?

Typo in Date.getDaysInMonth

I did not write a test to show the probable bug that this would cause, but the parameter month to the implementation for Date.getDaysInMonth() is referenced by mont.

Line: 231 (presently)

getDaysBetween returns wrong number

console.log(new Date(2014,6,9,14,0,0).getDaysBetween(new Date(2014,6,10))); // returns 0 instead of 1

I guess there is some problem if hours are not exactly 0

Browser support?

Is there data on which browsers are supported? I don't see it in the documentation or readme, or am I missing it?

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.