jquery datepicker returns wrong year when using date format without year part - jquery-ui-datepicker

I use jquery datepicker (v1.8.20) with the date format set to 'D MM d' which looks like: 'Wed September 12'. When I select different year than the current one from a calendar, the getDate method returns me current year. Moreover, when I open calendar again, both selected day and month are preserved, but the year is changed for the current one. Generally it looks like year is set to current one when the date format does not contain it. Any idea how to fix that?

Datepicker stores value only in input element, so if you don't have a year in format string, datepicker merely doesn't store it. Here one of jquery.ui developers says, that this is not a bug and "Datepicker is only designed to pick a full date".
Anyway, I had the same problem in my project, and solved it by ugly trick, that forces datepicker to store full date:
(function($) {
$.datepicker._selectDateParent = $.datepicker._selectDate;
$.datepicker._adjustInstDateParent = $.datepicker._adjustInstDate;
$.datepicker._adjustInstDate = function(inst, offset, period) {
var fullDate = inst.input.data('fullDate');
if(fullDate && !period) {
inst.drawYear = inst.currentYear = fullDate.getFullYear();
}
this._adjustInstDateParent(inst, offset, period);
};
$.datepicker._selectDate = function(id, dateStr) {
var target = $(id);
var inst = this._getInst(target[0]);
var date = this._daylightSavingAdjust(new Date(inst.currentYear, inst.currentMonth, inst.currentDay));
inst.input.data('fullDate', date);
this._selectDateParent(id, dateStr);
}
})(jQuery);
I've tested it only with datepicker 1.9.0+ and I'm not sure, that this is a good solution, but it works for me :)

Related

Reduce a time stamp to its date

I am trying to compare current date with report date. The report date comes in this format: "2022-05-30 00:00:00.000000", whereas the current date I want to be in YYYY-MM-DD.
How can I either change the format, or strip the clock time from the timestamp?
var moment = require('moment');
var currentDate = moment().format("YYYY-MM-DD")
const jsonData = pm.response.json();
let date = (jsonData['data'][0]['date'])
pm.test("Fresh data is available", function () {
pm.expect(date).to.eql(currentDate);
});
As the datatype of the Date is string so you can use .substring() to extract only the portion you want. And in case of date, you want first 10 characters, i.e.,'2022-05-30' instead of '2022-05-30 00:00:00.000000'.
So you can use : jsonData.data[0].date.substring(0,10)

Selection for last date is still retaining in multiselect jquery-Ui-Datepicker

I am using Jquery-ui-datepicker as inline with multidate: true. when i select multiple dates and click outside the calendar still the last selected date is highlighted. I want to remove the highlight when we click outside the calendar after the dates got selected
I have tried to remove the active class from the date, but what is ts doing is from UI the highlight is going Off, but the datepicker is not updated, so when i click on other date both the dates are getting highlighted.
Another issue is like if i call update method, it is working fine but the thing is the calendar is resetted to current month.
I want to stay in the month where the user selected the dates, for example, i am selecting dates in November, if i update the datepicker, it is showing the "September" month which is current month.
$('.date').datepicker()
var new_options = {
multidate: true,
startDate: new Date,
todayHighlight: true,
// daysOfWeekHighlighted: [0, 6],
endDate: new Date('12/31/2050'),
dateFormat: 'mm-dd-yyyy',
beforeShowDay: function(date){
let formattedDate = moment(date).format("MM/DD/YYYY");
if ($.inArray(formattedDate, already_active_generalHolidays) != -1){
return {
classes: 'generalHolidayClass'
};
}
}

Swift 3 FSCalendar select all dates by default

I am able to select certain dates while loading the FSCalendar. But I want to load calendar with all dates selected.
var attendance_list = [NSManagedObject]();
//After loading data
for attendanceObj in attendance_list{
let createdDate = attendanceObj.value(forKey: "date") as! String;
self.calendar.select(Utils.convertStringToDate(createdDate))
}
Above code is for selecting dates from an array.
How can I select all dates without any array ?
I wouldn't recommend selecting all dates since the beginning of time to the end of the foreseeable future (also, you cannot do it without an array). What I would recommend is getting the beginingOfMonth and endOfMonth (both methods are available) in currentPageDidChange (another available method). Then set the dates between these two dates to selected inside a loop.

calculation of differences of dates in Swift 3

I have created 3 outlets for labels,1 action for button and 1 outlet for UIDatePickerView. lblField displays the current date with month, date and year[January 25, 2017] format. lblField2 displays the selected dates by the user after pressing dueDate action. Now, I need to calculate the differences between the current date and selected date i.e (lblField2 - lblField) in lblField3. How can i show the difference value in months and days in Swift3. I need strictly for Swift3 ?
You can use dateComponentsFormatter for that.
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.month,.day]
formatter.maximumUnitCount = 2
formatter.unitsStyle = .full
lblField3.text = formatter.string(from: Date(), to: datePicker.date) ?? ""
If you want difference with month and day specific that you can use Calendar this way.
let components = Calendar.current.dateComponents([.month, .day], from: Date(), to: datePicker.date)
let dayDifference = components.day!
let monthDifference = components.month!
var date = NSDate().dateStringWithFormat(format: "dd.MM.YYY")
print(date)
15.02.2017
this will be the date formater easy and new single line sytax

Mongoose/MongoDB regex find query using variable

I'm trying to perform a find query in mongoDB (using the mongoose framework with nodejs and express) using a regex expression based on a variable. I'm partially able to query correctly using a static string hard-coded into the code but I need to perform the query using a variable who's value is constantly changing.
The query uses three fields (author, date and updated date) and should go through all documents in a scheme and find all documents where 'date' or 'updated date' is like the variable currentDate (always formatted as YYYY-mm-dd) and where author is authorname.
The main issue is that mongoDB stores the dates with ISO format. On execution the string variable (currentDate (YYYY-mm-dd)) is formatted to ISO which leads it to only "hit" documents with time 00:00.000Z as ISO formats 2015-09-17 to be 2015-09-17 00:00:00.000Z (not taking timezone into consideration).
The query:
var currentDate = yyyy-mm-dd;
Scheme.find({$and:[{'local.author': author},
{$or: [{'local.date': new RegExp(currentDate)},
{'local.updated_date':new RegExp(currentDate)}]}]}
I've also tried with the $gte and $lte variables (where $gte = date today and &lte = date tomorrow (00:00)) but I couldn't get it to work, I met the wall trying to perform the regex.
I hope there is a brilliant regex expert out there who can help, thank you! :)
I figured it out without all the complexity.
var tomorrow = new Date();
var today = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0,0,0,0);
today.setHours(0,0,0,0);
Scheme.find({$and:[{'local.author': author},{$or: [{'local.date': { $gt: today, $lt: tomorrow } },{'local.updated_date':{ $gte: today, $lt: tomorrow }}]}]}