便當盒

  • Random
  • Archive
  • RSS
  • Ask me anything

jQuery datetimepicker range select example

使用 jQuery datetimepicker 官網的範圍選擇範例遇到時間會自動消失的問題。

就是如果另外一個日期時間欄位(B)有預設值,在 B 的 datetimepicker 尚未顯示前就已經在 A 選好的值的話,會造成 B 的時間消失,且 A 跟 B 的 minDate/maxDate 效果會出不來。

以下是防止這個問題出現的範例,另外根據個人喜好加上限制時間的 minDateTime/maxDateTime 選項:

var dates = $('#from, #to').
  each(function(){
    if ( this.value ) {
      var option = this.id === 'from' ? 'minDate' : 'maxDate';

      $('#from, #to').not(this).
        attr('time:'+option, 'new Date("'+this.value+'")').
        attr('time:'+option+'Time', 'new Date("'+this.value+'")');
    }
  }).
  datetimepicker({
    dateFormat: 'yy-mm-dd',
    timeFormat: 'hh:mm:ss',
    changeMonth: true,
    alwaysSetTime: true,
    changeYear: true,
    hourGrid: 4,
    minuteGrid: 10,
    onSelect: function(selectedDatetime, inst) {
      var $_ = $(this),
          option = this.id === 'from' ? 'minDate' : 'maxDate',
          date = $_.datetimepicker('getDate'),
          other = dates.not(this);

      if ( other.val()) {
        var otherDate = other.datetimepicker('getDate');

        // limit current input
        if (
          ( this.id === 'from' && date.getTime() > otherDate.getTime()) ||
          ( this.id === 'to' && date.getTime() < otherDate.getTime())
        ) {
          date = new Date(otherDate.getTime());
          $_.datetimepicker('setDate', date);
        }

        // set other date
        other.
          datetimepicker('setDate', otherDate);
      }
      other.
        datetimepicker('option', option, date ).
        datetimepicker('option', option+'Time', date );
    }
  });
    • #jquery
    • #datetimepicker
    • #javascript
  • 3 months ago
  • 19
  • Comments
  • Permalink
  • Share
    Tweet

19 Notes/ Hide

  1. practicing90fg liked this
  2. communityto69 liked this
  3. castelinoui5 liked this
  4. nahoya posted this

Recent comments

Blog comments powered by Disqus
← Previous • Next →

About

Avatar

Me, Elsewhere

  • @BeataLyn on Twitter
  • Facebook Profile
  • Google
  • beata on github

Twitter

loading tweets…

Following

I Dig These Posts

See more →
  • Photoset via sonic1993


    this is Deth-Chu in order Nathan-chu, Toki-chu, Swis-chu, Pickles-chu, Murder-chu

    Photoset via sonic1993
  • Post via evenwu
    要求手繪 Logo 字體的迷思

    這是一個 logo 設計上的迷思,我常常聽到客戶要求:

    不要使用市面上的字體,那會跟別人一樣,我要「完全手繪的字體」。

    透過長年的訪談結果,他們的迷思通常是:

    • 字體好貴
    • 現成的字體會跟別人一樣

    ...

    Post via evenwu
  • Post via vincicat
    heroku 的 custom domain 與 Google App (on namecheap)

    heroku的DNS其實有點麻煩。

    因為Official給的”proxy.heroku.com.” (CNAME) 是不能設在 “@” (naked hostname) 上的…設了的話...

    Post via vincicat
  • Post via vincicat
    Bootstrap神器

    Twitter Bootstrap很強大大家都知道的吧。隨著Bootstrap的人氣也有不少Tool去支援:

    • 針對Bootstrap的Icon Font FontAwesome
    • Theme Bootswatch.com
    • Button...
    Post via vincicat
  • Link via jiwostudio
    "How do I go back?"

    Chris Pirillo 的爸爸使用 Windows 8 的情形。

    幸好我爸是用 iPad 的。

    4 分 13 秒左右:

    Who puts this out?

    Microsoft.

    They’re trying to drive me to...

    Link via jiwostudio
  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

創用 CC 授權條款 本著作係採用創用 CC 姓名標示-相同方式分享 3.0 Unported 授權條款授權 - . Effector Theme by Carlo Franco.

Powered by Tumblr