Unable to Select the month of a jquery datepicker using the Selenium - jquery-ui

I am trying to select a custom date in the datepicker present in the jqueryui.com site, it is able to process the year , but the code that i written for the month is not working.Please find the code that i have used:-
private static void PickDate(WebDriver driver, String day, String mon, String year) throws Exception {
while (year != driver.findElement(By.cssSelector("span.ui-datepicker-year")).getText())
{
if (Integer.parseInt(year) < Integer.parseInt(driver.findElement(By.cssSelector("span.ui-datepicker-year")).getText()))
{
driver.findElement(By.cssSelector("a.ui-datepicker-prev")).click();
System.out.println(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText());
}
else if (Integer.parseInt(year) > Integer.parseInt(driver.findElement(By.cssSelector("span.ui-datepicker-year")).getText()))
{
driver.findElement(By.cssSelector("a.ui-datepicker-next")).click();
}
}
while (mon != (driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText()))
{
if (driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText() != "January")
{
driver.findElement(By.cssSelector("a.ui-datepicker-prev")).click();
}
if (mon != driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText())
{
driver.findElement(By.cssSelector("a.ui-datepicker-next")).click();
}
}

If the purpose is to select a date in the datepicker, I would rather use execute script than click back and forth. :)
Something like:
((JavascriptExecutor) driver).ExecuteScript("$('#datepicker').datepicker('setDate', new Date(year, month, day))")
If you want to click on the previous/next button, then:
Because your code looks quite complicated, so I would like to provide another approach, which I believe that will be much simpler.
Notice that when you click on the previous/next button, it will move forward/backward by 1 month. So it's better if you firstly calculate how many months in difference between the date you want to select (month and year) and the current shown date on the DatePicker.
If you write in java, you can use something like:
YearMonth selectDate = YearMonth.of(year, month); // year, month are integer
int dp_Year = Integer.parseInt(driver.findElement(By.cssSelector("span.ui-datepicker-year")).getText());
int dp_Month = // convert it yourself
YearMonth dp_Date = YearMonth.of(dp_Year, dp_Month);
int month_diff = dp_Date.until(selectDate, ChronoUnit.MONTHS);
Based on month_diff and its sign (+ or -), you will know which button you need to click on and how many times.
I don't have java to try out, but I hope that the code above will work.

Related

How to match a string above a given cell index

I have a schedule like this and somehow determined the position of Alice in B7.
Given the index B7, how can I find the matching date? Here it would the first cell above Alice that starts with October.
SUGGESTION
It looks it would be quite tricky to use just solely the pre-defined functions to achieve this, given that the total number of names per dates are random (some dates has only 2 names and other has 3 based on your sample sheet).
You can try using a custom formula for this method, just copy and paste the sample functions below as a bound script to your sheet file then adjust it your setup:
function FINDDATE(cell) {
var sheet;
if(cell.split("!").length==1){
sheet = SpreadsheetApp.getActive().getActiveSheet();
}else{
sheet = SpreadsheetApp.getActive().getSheetByName(cell.split("!")[0]);
}
var selectedLoc = sheet.getRange(cell);
for(i=selectedLoc.getRow(); i != 1; i--){
var dateFound = isValidDate(sheet.getRange(i,selectedLoc.getColumn()).getValue());
if(dateFound == true){ //Return the date of the very first detected as date above the index
var res = Utilities.formatDate(sheet.getRange(i,selectedLoc.getColumn()).getValue(), Session.getScriptTimeZone(), "MMMM d");
return res.toString();
}
}
}
function isValidDate(d) { //Check if a cell is a date, reference from https://stackoverflow.com/a/17110249
if ( Object.prototype.toString.call(d) !== "[object Date]" )
return false;
return !isNaN(d.getTime());
}
SAMPLE DEMONSTRATION:
Two methods below on how to use the FINDDATE custom function:
Method 1:
Method 2:
Method 3: Using FINDDATE on a different sheet

How to disable the past dates in the Kendo date picker?

How to disable the past dates in the Kendo date picker ? ( Date Picker validation)
That will allow the user to select only the current date and future date.
In the HTML :
#Html.EditorFor(Model => Model.AppointmentDate)
In the JQuery :
$('#AppointmentDatee').data('kendoDatePicker')
The shortest way to disable past dates is using min parameter with current date value:
var presentDate = new Date();
$(function () {
var datepicker = $('#AppointmentDate').kendoDatePicker({
value: presentDate,
min: presentDate,
}).data('kendoDatePicker');
});
If you're using Razor with #Html.Kendo() helper, use DatePickerBuilderBase.Min() method:
#(Html.Kendo().DatePicker().Name("AppointmentDate").Min(DateTime.Today))
However, the min parameter will remove all disabled past dates (i.e. they're not shown in calendar view). If you want to show disabled dates but the user cannot interact with them (by clicking the date), use k-state-disabled CSS class in empty option inside month parameter:
var datepicker = $('#AppointmentDate2').kendoDatePicker({
value: presentDate,
min: presentDate,
month: {
empty: '<div class="k-state-disabled">#= data.value #</div>'
}
}).data('kendoDatePicker');
If #(Html.Kendo()) helper is used, use DisabledDates to call a function which disables past dates like example below:
<script>
var getPastDates = function(begin, end) {
for (var dtarr = [], date = start; date < end; date.setDate(date.getDate() + 1)) {
dtarr.push(new Date(dt));
}
return dtarr;
}
function disablePastDates(date) {
var pastDate = getPastDates(new Date('0001-01-01T00:00:00Z'), new Date());
if (date && compareDates(date, dates)) {
return true;
}
else {
return false;
}
}
function compareDates(date, dates) {
for (var i = 0; i < dates.length; i++) {
if (dates[i].getDate() == date.getDate() &&
dates[i].getMonth() == date.getMonth() &&
dates[i].getYear() == date.getYear()) {
return true;
}
}
}
</script>
Helper usage:
#(Html.Kendo().DatePicker().Name("AppointmentDate").DisableDates("disablePastDates"))
Working examples:
JSFiddle demo 1 (hidden past dates)
JSFiddle demo 2 (grayed-out past dates)
References:
Kendo.Mvc.UI.Fluent.DatePickerBuilderBase.Min(DateTime)
Show Out-of-Range Dates as Disabled
Kendo MVC DatePicker - Disable dates
Similar issue (with different approach):
How to disable past dates without hiding them in Kendo date picker?
if you use jquery for your kendoDatePicker , this code may help you!
$("#MyDatapickerElement").kendoDatePicker({
value: new Date(),
disableDates: function (date) {
if (date <= new Date()) {
return true;
} else {
return false;
}
}
});
If using Html.Kendo().DatePicker() you can show the disabled dates using the MonthTemplate. Example below shows the Minimum Date set to DateTime.Today and sets the MonthTemplate to show past dates as disabled.
Html.Kendo().DatePicker()
.Name("MyDate")
.Min(DateTime.Today)
.MonthTemplate(m=>m
.Empty("<div class=\"k-state-disabled\">#= data.value #</div>")
)

How To Get Thursdays Of Year Using Carbon

I want to get all the Thursdays of a year using Carbon and save it on DB.
so far I could only store the Thursdays of current month. please advice me how to make it for the whole year once.
Current code :
public function getThursdays()
{
return new \DatePeriod(
Carbon::parse("first thursday of this month"),
CarbonInterval::week(),
Carbon::parse("first thursday of next month")
);
}
public function handle()
{
$thursdays = $this->getThursdays();
foreach ($thursdays as $thursday)
{
$Weeks = new Week();
$Weeks->week_starting_date = $thursday;
$Weeks->save();
}
}
You can get the number of weeks per year firstly using WEEKS_PER_YEAR, e.g :
$weeks_per_year = Carbon::WEEKS_PER_YEAR;
After that get the first thursday of year using :
$first_thursday = Carbon::parse("first thursday of this year");
Then loop through all the weeks adding every time one week using Carbon function addWeeks(1) to get the thursday of the next week and so :
for($i=1;$i<$weeks_per_year;$i++)
{
array_push( $thursdays, $first_thursday->addWeeks(1)->toDateString() );
}
FULL code :
$weeks_per_year = Carbon::WEEKS_PER_YEAR;
$first_thursday = Carbon::parse("first thursday of this year");
$thursdays = [$first_thursday->toDateString()];
for($i=1;$i<$weeks_per_year;$i++)
{
array_push( $thursdays, $first_thursday->addWeeks(1)->toDateString() );
}
Output :
An array of all the year thursdays in following format yyyy-mm-dd :
["2016-01-07","2016-01-14","2016-01-21", ... ,"2016-12-29"]
Or you can store dates directely in you DB instead of returning an array like :
for($i=1; $i<$weeks_per_year;$i++){
$Weeks = new Week();
$Weeks->week_starting_date = $first_thursday->addWeeks(1)->toDateString();
$Weeks->save();
}
Hope this helps.

KendoUI 2013.3.1324: timezoneoffset and negative milliseconds value

I have updated my project to version 2013.3.1324 from 2013.3.1119 (with ASP.NET MVC wrappers)
And I saw the following after update:
DateTime is passed to the client as
"/Date(-498283200000)/"
if less than 1970 year and
"/Date(498283200000)/"
if more that 1970 year
I have found a strange code in the kendo.all.js file
dateRegExp = /^\/Date\((.*?)\)\/$/,
tzOffsetRegExp = /[+-]{1}\d+/,
...
if (value && value.indexOf("/D") === 0) {
date = dateRegExp.exec(value);
if (date) {
date = date[1];
tzoffset = tzOffsetRegExp.exec(date);
date = parseInt(date, 10);
if (tzoffset) {
date -= (parseInt(tzoffset[0], 10) * kendo.date.MS_PER_MINUTE);
}
return new Date(date);
}
}
Debug info:
Initial value:
Parsed date value:
Parsed tzo value:
And finally, result date value:
Actually I don't need time, only Date. Model property type is regular DateTime.
Also I can't find any issues with this release on the Kendo site.
What I'm doing wrong and what I need to do? (changing Kendo source is not an option I think...)
Example:
Live demo: http://jsbin.com/vebed/2/edit?html,js,output
The following:
alert(kendo.parseDate("/Date(-498283200000)/"))
shows
Thu Mar 18 1954 22:00:00 GMT+0200 (FLE Standard Time)
with the latest official version of Kendo UI.
Make sure you are not using an older version.
Here is a live demo: http://jsbin.com/vebed/1/edit
Issue was fixed in the Internal build 2013.3.1408
New code is:
if (value && value.indexOf("/D") === 0) {
date = dateRegExp.exec(value);
if (date) {
tzoffset = date = date[1];
date = parseInt(date, 10);
tzoffset = tzoffset.substring(1).split(signRegExp)[1];
if (tzoffset) {
date -= (parseInt(tzoffset, 10) * kendo.date.MS_PER_MINUTE);
}
return new Date(date);
}
}

Not able to read the time from native calendar event

I have created a appoitment for particular date and time in Blackberry calendar,i am trying to read date and time using the following code,but its showing the error.
private void getEvents() {
try {
EventList eventList = (EventList)PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.READ_ONLY);
Enumeration events = eventList.items();
while (events.hasMoreElements()) {
Event event = (Event)events.nextElement();
if(eventList.isSupportedField(Event.ALARM) && event.countValues(Event.ALARM) > 0) {
long alarm = event.getDate(Event.ALARM, 0);
System.out.println(alarm);
}
}
}
i am not sure what is wrong in if loop
The field Event.ALARM contains:
Field specifying a relative time for an Alarm for this Event. Data for
this field is expressed with an INT data type. The alarm is expressed
in seconds and derived by subtracting the alarm value from every
date/time occurrence of this Event. For example, if this field has a
value of 600, then the alarm first occurs 600 seconds before the
date/time value specified by Event.START. For re-occurrences of the
event, the alarm is calculated by subtracting the stored value from
the date/time of the specific event occurrence.
So you need to get the value from the field Event.START for the Date/Time of the Event start. You can then subtract the value of Event.ALARM (as seconds) from the start Date/Time to get the time for any requested reminder.
long start = event.getDate(Event.START);
int alarm = event.getDate(Event.ALARM);
if (alarm > 0) {
long reminderTime = start - (long)alarm * 1000L;
...
}
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm");
String dateString = sdf.formatLocal(start);

Resources