Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datepicker - How to change "today" date? [duplicate]

Is there a way to change the "today" date in jquery.ui datepicker? With today, I mean today (class="ui-datepicker-today") and not the minDate or the current-selected date!

I figured out, that datepicker uses the system time for default values. Now I get the current date from my server and set it to my minDate. But I didn't find a way to set the today-date... It still uses my system date (there are some users out there whose systemdate is in the year 2000 or earlier).

Can you help me, please?

like image 306
Newbie Avatar asked Mar 25 '10 14:03

Newbie


People also ask

How set today's date in jQuery DatePicker?

Set Current Date in DatePicker using jQueryvar now = new Date(); var day = ("0" + now. getDate()).

How can set current date in textbox using jQuery?

You must FIRST call datepicker() > then use 'setDate' to get the current date. $(". date-pick").

How to get date value in jQuery?

get current date in jquery in dd/mm/yyyy format” Code Answer. var mm = String(today. getMonth() + 1).

What is RTL DatePicker?

Right-to-left (RTL) support reflects the ability of a widget to render its content in a right-to-left direction for right-to-left languages, such as Arabic, Hebrew, Chinese, or Japanese. For more information, refer to: RTL support by the DatePicker (demo)


2 Answers

Based on the source, it doesn't look good.

var today = new Date();

That is always going to be the user's system date.

If you absolutely must fix this, look down a line; you could override $.datepicker._daylightSavingAdjust and return the date from your server *

But this is a bad idea. Why on earth do your users have their system date so messed up? Why is that your problem? If they complain, tell them to fix their clocks.

* I can't emphasize how bad an idea this is. That function is used to normalize most of the dates in the datepicker, so you'll have to write some pretty convoluted logic to keep from messing everything up. That may not even be feasible.

like image 92
noah Avatar answered Oct 13 '22 11:10

noah


A small tweak to the selected answer in jQuery UI DatePicker change highlighted "today" date

// Get users 'today' date
var localToday = new Date();
localToday.setDate(tomorrow.getDate()+1); // tomorrow

// Pass the today date to datepicker
$( "#datepicker" ).datepicker({
    showButtonPanel: true,
    localToday: localToday    // This option determines the highlighted today date
});

I've overridden 2 datepicker methods to conditionally use a new setting for the "today" date instead of a new Date(). The new setting is called localToday.

Override $.datepicker._gotoToday and $.datepicker._generateHTML like this:

$.datepicker._gotoToday = function(id) {
    /* ... */
    var date = inst.settings.localToday || new Date()
    /* ... */
}

$.datepicker._generateHTML = function(inst) {
    /* ... */
    tempDate = inst.settings.localToday || new Date()
    /* ... */
}

Here's a demo which shows the full code and usage: http://jsfiddle.net/NAzz7/5/

like image 33
Cory Danielson Avatar answered Oct 13 '22 11:10

Cory Danielson