Git Product home page Git Product logo

stringdate.js's Introduction

String-Date JavaScript Library

The stringdate.js provides functions handling String as a Date.

Usage

Valid Format

Valid format is "yyyy-MM-dd" in general. "yyyy-MM" is allowed for getLastDate() and isLeapYear(). "yyyy" is allowed for isLeapYear() only.

Note that the string should be valid syntactically and semantically. It means "2015-04-31" is not valid in stringdate.js while it makes a date of May 1st, 2015 in vanilla JavaScript.

All functions in below should applied on valid format, otherwise it throws Error.

Getting Year, Month, Date, and Day

String.prototype.getYear() : Returns year of 4-digits.

String.prototype.getMonth() : Returns month between 1 and 12. : Note that vanilla JavaScript Date object's getMonth() returns integer between 0 and 11.

String.prototype.getDate() : Returns date of month between 1 and 31.

// get year
"2015-03-14".getYear(); // 2015

// get month
"2015-03-14".getMonth(); // 3

// get date of month
"2015-03-14".getYear(); // 14

// get day of week
"2015-03-14".getDay(); // 6

Dealing Last Date and Leap Year

String.prototype.getLastDate() : Returns last date of the month. Date format can be either "yyyy-MM-dd" or "yyyy-MM".

String.prototype.isLeapYear() : Determine whether the year is leap year or not. Date format can be one of "yyyy-MM-dd", "yyyy-MM", or "yyyy".

// get last date
"2015-02-01".getLastDate(); // 28
"2015-02".getLastDate(); // 28

// determine leap year
// 2015
"2015-02-01".isLeapYear(); // false
"2015-02".isLeapYear(); // false
"2015".isLeapYear(); // false
// 2016
"2016-02-01".isLeapYear(); // true
"2016-02".isLeapYear(); // true
"2016".isLeapYear(); // true

Calculating days between dates

String.prototype.getDaysFrom(target) : Returns days between this date and target date. It returns positive when target is past, or negative when target is future. You can use this function as subtraction operator -.

String.prototype.addDays(days) : Returns new date after/before the date. It returns future date for positive days, or past date for negative days. You can use this function as addition operator +.

// get days between two dates
"2015-03-31".getDaysFrom("2015-03-01"); // 30
"2015-04-01".getDaysFrom("2015-06-30"); // -90

// get date after/before a date
"2015-03-14".addDays(100); // "2015-06-22"
"2015-03-14".addDays(-28); // "2015-02-14"

Example

Count working days

var count = 0;
for (var date = "2015-04-01"; date <= "2015-06-30"; date = date.addDays(1))
    if (date.getDay() != 0 && date.getDay() != 6)
        count++;
console.log(count); // 65

License

MIT License

stringdate.js's People

Contributors

leafriend avatar

Stargazers

Edward Kim avatar

Watchers

James Cloos avatar  avatar

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.