GithubHelp home page GithubHelp logo

aliirz / datepicker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cwspear/datepicker

0.0 3.0 0.0 889 KB

The one and only datepicker with no dependencies. Comes in different flavors: Standalone, jQuery plugin and AngularJS directive.

JavaScript 85.66% HTML 6.66% CSS 7.68%

datepicker's Introduction

Datepicker

The one and only datepicker with no dependencies.

How to

Standalone:

<div id="my-datepicker"></div>

<script src="datepicker.min.js"></script>
<script>
  var elem = document.querySelector('#my-datepicker');

  new Datepicker(elem, {
    onDateChanged: function (selectedDate) {
      console.log(selectedDate);
    }
  });

  elem.datepicker.setSelectedDate(new Date('6/22'));
</script>

With AngularJS:

<sexy-datepicker data-ng-model="selectedDate" />

<script src="angular-1.1.5.js"></script>
<script src="angular.datepicker.min.js"></script>
<script>
  angular.module('myApp', ['sexyDatepicker'])
    .controller('myController', function ($scope) {
      $scope.selectedDate = new Date('6/22');
    });
</script>

With jQuery:

<div id="my-datepicker"></div>

<script src="jquery.js"></script>
<script src="jquery.datepicker.min.js"></script>
<script>
  $('#my-datepicker').datepicker({
    onDateChanged: function (selectedDate) {
      console.log(selectedDate);
    }
  });

  $('#my-datepicker').get(0).datepicker.setSelectedDate(new Date('6/22'));
</script>

Interacting with datepicker

Going back and forth in months programatically.

/* Create datepicker with current date set to today.
   July 1st. */
var datepicker = new Datepicker(...);

/* Select August 1st. */
datepicker.incrementMonth();

/* Change date to 2 months later. */
datepicker.incrementMonth(2);

/* Change it back to July 1st. */
datepicker.incrementMonth(-3);

/* Move to the next year. */
datepicker.incrementMonth(12);

/* Move back to the year selected before. */
datepicker.incrementMonth(-12);

Changing selected date.

/* New datepicker with today's date. July 1st. */
var datepicker = new Datepicker(...);

/* Make it select my birthday. */
var myBirthday = new Date('6/22/2013');
datepicker.setSelectedDate(myBirthday);

Re-build datepicker.

var datepicker = new Datepicker(...);
datepicker.build();

Remove datepicker completely.

var datepicker = new Datepicker(...);
datepicker.destroy();

Actually getting a date from datepicker

Each time the date changes, the onDateChanged option will be called with the selected date as its first argument.

/* Print the new date on the console */
new Datepicker(elem, {
  onDateChanged: function (newDate) {
    console.log(newDate);
  }
});

It'll also call onDateChanged when created, unless you pass a date in the options.

/* This will not trigger onDateChanged callback */
new Datepicker(elem, {
  selectedDate: new Date('6/22'),
  onDateChanged: function (newDate) {
    console.log(newDate);
  }
});

More options

Override names of months...

/* Months in spanish */
new Datepicker(elem, {
  onDateChanged: function (newDate) {
    console.log(newDate);
  }
  monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre']
})

...and names of days too.

/* Months and days in spanish */
new Datepicker(elem, {
  onDateChanged: function (newDate) {
    console.log(newDate);
  }
  monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre'],
  dayNames: ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado']
})

When using the AngularJS directive:

<!-- Add `month-names` and `day-names` attributes to element. -->
<sexy-datepicker month-names="months" day-names="days" />
angular.module('myApp', ['sexyDatepicker'])
  .controller('myController', function ($scope) {
    /* Create arrays with name on the $scope. */
    $scope.months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre'];
    $scope.days = ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'];
  });

To-do

  • Build with Grunt.
  • Add tests.
  • Something that seemed extremely important but I've forgotten.

Contributing

Seriously? Thank you!

Use it

Copyright (c) 2013 Federico Baña.

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.

datepicker's People

Watchers

Ali Raza avatar 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.