Coder Social home page Coder Social logo

tarunc / datepair.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jonthornton/datepair.js

0.0 3.0 0.0 265 KB

A javascript plugin for intelligently selecting date and time ranges, inspired by Google Calendar.

Home Page: http://jonthornton.github.io/Datepair.js

JavaScript 70.45% HTML 20.94% CSS 8.61%

datepair.js's Introduction

Datepair.js

See a demo and examples here

Datepair.js is a lightweight, modular javascript plugin for intelligently selecting date and time ranges, inspired by Google Calendar. It will keep the start and end date/times in sync and can set default values based on user action. There are no external dependencies, however it can easily be used with jQuery or Zepto. The plugin does not provide any UI widgets; it's preconfigured to work with jquery-timepicker and Bootstrap Datepicker, but you can use it with any datepicker or timepicker (or none at all).

Looking for jquery-datepair? Scroll down.

Requirements

Usage

Include dist/datepair.js or dist/jquery.datepair.min.js in your app.

var container = document.getElementById('container')
var datepair = new Datepair(container, options);

Where #container contains time/date <input /> elements with the appropriate class names. options is an optional javascript object with parameters explained below.

Note that Datepair is triggered by change events and won't work unless the container has some <input /> elements.

Options

  • anchor
    The input that will control the other input. One of "start", "end", or null. See demo page for more information. default: "start"

  • dateClass
    Class name of the date inputs, if any.
    default: "date"

  • defaultDateDelta
    Fill in the second date value with the specified range when the users selects the first date. Value is in days. Set this to null to disable automatically setting the second date.
    default: 0

  • defaultTimeDelta
    Fill in the second time value with the specified range when the users selects the first time. Value is in milliseconds; set this to 7200000 for a 2 hour range, for example. Set this to null to disable automatically setting the second time.
    default: 0

  • endClass
    Class name of the range end input(s).
    default: "end"

  • parseDate
    A function that takes a jQuery element for a date input and returns a local time Date object representing the date input value.
    default: function for Bootstrap Datepicker

  • parseTime
    A function that takes a jQuery element for a time input and returns a local time Date object representing the time input value. See example page for more info.
    default: function for jquery-timepicker

  • setMinTime
    A function that takes a jQuery element for a time input and a local time Date object representing the time, and sets the timepicker minTime value.
    default: function for jquery-timepicker

  • startClass
    Class name of the range start input(s).
    default: "start"

  • timeClass
    Class name of the time inputs, if any.
    default: "time"

  • updateDate
    A function that takes a jQuery element for a date input and a local time Date object representing the date, and sets the input value.
    default: function for Bootstrap Datepicker

  • updateTime
    A function that takes a jQuery element for a time input and a local time Date object representing the time, and sets the input value.
    default: function for jquery-timepicker

Methods

  • getTimeDiff
    Get the date/time range size, in milliseconds.

     var milliseconds = datepair.getTimeDiff();
  • refresh
    Call this method if you programmatically update the date/time fields after first initialization of datepair.

     $('#someInput').val(someValue)
     datepair.refresh();
  • remove
    Unbind the datepair functionality from a set of inputs.

     datepair.remove();

Events

  • rangeError
    Fired after the user interacts with the datepair inputs but selects an invalid range, where the end time/date is before the start.

  • rangeIncomplete
    Fired after the user interacts with the datepair inputs but one or more empty inputs remain. Unpaired inputs (such as a start date with no corresponding end date) will not be taken into account.

  • rangeSelected
    Fired after the user interacts with the datepair inputs and all paired inputs have valid values.

jQuery Plugin

Datepair.js includes an optional jQuery interface that can simplify usage when working with jQuery or Zepto. To activate, include both datepair.js and jquery.datepair.js, or just jquery.datepair.min.js. (The minified version includes both scripts.)

Usage

$('#container').datepair(options);
var milliseconds = $('#container').datepair('getTimeDiff');
$('#container').datepair('remove');
$('#container').datepair('refresh');

jQuery-UI Datepicker

By default, Datepair.js is configured to work with Bootstrap Datepicker. This is different from jQuery UI Datepicker. To use jQuery UI Datepicker, override the parseDate and updateDate methods:

$('#some-container').datepair({
    parseDate: function (el) {
        var val = $(el).datepicker('getDate');
        if (!val) {
            return null;
        }
        var utc = new Date(val);
        return utc && new Date(utc.getTime() + (utc.getTimezoneOffset() * 60000));
    },
    updateDate: function (el, v) {
        $(el).datepicker('setDate', new Date(v.getTime() - (v.getTimezoneOffset() * 60000)));
    }
});

Packaging

https://www.npmjs.com/package/datepair.js
npm install --save datepair.js

Help

Submit a GitHub Issues request.

Development Guidelines

  • Install dependencies (grunt) npm install
  • To build and minify, run grunt
  • Use grunt watch to continuously build while developing

Datepair.js follows semantic versioning.


This software is made available under the open source MIT License. © 2014 Jon Thornton.

datepair.js's People

Contributors

blinskey avatar jhegeman avatar jkallay1 avatar jonthornton avatar kylethielk avatar monkbroc avatar rhec avatar tarunc avatar

Watchers

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