I used the following code to add one year from input date:
this.maxDate = this.drop.startDate;
this.maxDate.setDate(this.maxDate.getDate() + 365);
In this context, drop.startDate is the input variable. But when I run this code, 1 year will be added to the maxDate variable, and drop.startDate value will changed to maxDate value. Look at the images. Any guidelines guys?
Before run the code
After run the code
You have to 'clone' the date object. Otherwise the startDate will have the same reference as maxDate
this.maxDate = new Date(this.drop.startDate.getTime());
Related
I'm trying to get the current date - 3 months and use it in a postman Pre-request script. I'm told it uses javascript, but it doesn't seem to be working.
The error I get is:
There was an error in evaluating the Pre-request Script: TypeError:
startDate.setMonth is not a function
Here is what I have:
// setup start date
var startDate = Date();
startDate.setMonth(startDate.getMonth() - 3);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax
JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.
so
Date();
needs to be
new Date();
Try change var startDate = Date(); to var startDate = new Date();
As an alternative, Postman comes with the moment module built in so you could do something like this:
var moment = require("moment")
var startTime = moment().subtract(3, 'months')
Or you could obviously use native JavaScript, worth knowing a couple of different ways though.
I'm trying to use the Calendar API to pull up some events based on datepicker output. The issue that i'm facing with my AppScript is formatting properly the value that i get from the datepicker that will serve as input for the getEventsForDay() function.
function testing(){
var z = CalendarApp.getCalendarsByName("X")[0];
var date = new Date('2016-10-12');
var dateformatted =Utilities.formatDate(date), "GMT", "yyyy-MM-dd hh:mm:ss a");
var a = z.getEventsForDay(dateformatted, {search: 'OOO'});
The output of a into this scenario is a empty object - which is expected because this formatting is not working at all. (i've read 1000 posts that this should work).
For context as well, i have one working example with today's date, which it works fine becase the input is a new Date(). Here you go:
var datetoday = new Date();
var b = z.getEventsForDay(datetoday, {search: 'OOO'});
Any ideas on what i'm missing here?
Thanks in advance.
Since new Date() returns the Date object in UTC and not the local timezone, you may be querying the wrong day and hence the empty object if the other day does not have any events.
You can covert the date to the current timezone like
date.setTime(date.getTime() + date.getTimezoneOffset()*60*1000)
This should return the date in the local timezone and you will get the events for the correct day.
Hope it helps
Made some changes and now this works on the App Script:
var t = "2016-10-01";
var q = t.replace(/-/g,"/");
var a = new Date(q);
a.setTime(a.getTime() + a.getTimezoneOffset()*60*1000)
var w = z.getEventsForDay(a, {search: 'OOO'});
Logger.log(a);
Logger.log(w);
i have a problem when using JQXDateTimeInput in javascript
i have a date using JQXDateTimeInput which element id = datefrom
and i want to change the other JQXDateTimeInput which element id = dateto
when user changed datefrom by addition 6 month and subtract one day from datefrom that changed by user.
can some help me?
thanks
Yes, I understand what you want ot do. You have to use their API to
catch event of changing #datefrom with valueChanged for example
get selected date and add 6 month
assing new value with value property or setDate method to
#dateto
So the full code
// catch event ("change" event also works here)
$('#datefrom').on('valueChanged', function (event) {
// get selected date
var newDate = new Date(event.args.date);
// add 6 month
newDate.setMonth(newDate.getMonth() + 6);
// set another input's date
$('#dateto').jqxDateTimeInput({value: newDate});
// or
// $('#dateto').jqxDateTimeInput('setDate', newDate);
});
FIDDLE
I have a form, where I need to do some validations by getting the current date and next date separately using javaScript.
I tried this code but it does not work correctly.
var currentDate = new Date();//get current date
var validdate=currentDate;
validdate.setDate(validdate.getDate()+1);
But what happens is when valid date changes to +1, then current date also changes to +1.
But I need it separately, means when I print the output: I get
currentDate = 5th May 2014;
validdate = 6th May 2014;
Then when again I print currentDate, it changes to "6th May 2014".
How to get the current date and next date without affecting each other?
instead of validdate = currentDate; try validdate = new Date();.
Your problem happens because in javascript when you pass an object to a value it is passed by reference and not by value. So when you change it you change even the original object because you don't actually have two different objects.
you are refering to same object instance for currentDate and validdate.
initialise them seperatly like below
currentDate = new Date();
validdate = new Date();
also rename your validdate to validDate for more readability.
to make a new copy of a Date object use following code
var validdate = new Date(currentDate.getTime());
now when you modify the validdate object it will not update currentDate
Try This
var currentDate = new Date();//get current date
alert(currentDate.toString());
var validdate=new Date();
validdate.setDate(currentDate.getDate()+1);
alert(validdate.toString());
alert(currentDate.toString());
I have the jquery ui datepicker in my page and use two input elements id of 'startDate' and 'endDate'. My javascript has a function 'setRange()' in which min date is defined but I want to define max date that should be startDate+6 days only. I mean user must not select the date after 6 days of start date.
Please Help me. Thanks
I built something similar the other day. Basically you need to set the maxDate option on the endDate element, every time startDate is changed.
I like using moment.js for dates, since it allows you to do things like date.add('days',6)
Here's something to start you off:
$(function() {
$('#start_date').change(function() {
var start = $(this).val();
var maxDate = new Date(); // I'll leave this to you...
$('#end_date').datepicker('option','maxDate',maxDate);
}).trigger('change'); // this sets the constraint on load, too
});