I have a test written in Jasmine test runner:
it("Expect 'due date' to be 14 days from today", function () {
var dateNow = new Date();
scope.dateOfService(dateNow);
expect(scope.DueDate == new Date(dateNow.setDate(dateNow.getDate() + 14))).toBeTruthy();
});
However this always returns false? Is there something I'm doing incorrect?
To 'debug' this I also ran:
expect(scope.DueDate).toBe(new Date(dateNow.setDate(dateNow.getDate() + 14)));
And this returns:
Expected Date(Tue Nov 11 2014 08:20:23 GMT+0000 (GMT Standard Time)) to be Date(Tue Nov 11 2014 08:20:23 GMT+0000 (GMT Standard Time))
var dateNow = new Date();
scope.dateOfService(dateNow);
var dateReturned = new Date(scope.DueDate);
var dateAdd = new Date(dateNow.setDate(dateNow.getDate() + 14));
expect(dateReturned).toEqual(dateAdd);
The main point was to ensure it was .toEqual rather than .toBe.
Related
I tried the following code to set the startDate and endDate of a PickerComponent:
CSS:
#Constants {
includeNativeBool: true;
lightweightPickerBool: true; /* It's necessary to use the "setStartDate" and "setEndDate" of the Picker */
}
MainClass:
Form f = new Form(new TextModeLayout(1, 1));
int MINAGE = 13;
Calendar minAge = Calendar.getInstance(); // it's not a singleton, https://stackoverflow.com/questions/6055112/why-can-i-have-only-one-instance-of-calendar-object
minAge.setTime(new Date()); // current time
int year = minAge.get(Calendar.YEAR) - MINAGE;
minAge.set(Calendar.YEAR, year);
Date endDate = minAge.getTime();
Calendar maxAge = Calendar.getInstance();
maxAge.setTime(new Date());
maxAge.set(Calendar.YEAR, 1930);
maxAge.set(Calendar.MONTH, 1);
maxAge.set(Calendar.DAY_OF_MONTH, 1);
Date startDate = maxAge.getTime();
PickerComponent inputCmp = PickerComponent.createDate(endDate).label("Birthday");
Log.p("startDate: " + startDate.toString());
Log.p("endDate: " + endDate.toString());
inputCmp.getPicker().setStartDate(startDate);
inputCmp.getPicker().setEndDate(endDate);
f.add(inputCmp);
f.show();
but when I try to select the date in the picker, I get:
[EDT] 0:0:0,293 - startDate: Sat Feb 01 12:30:58 CET 1930
[EDT] 0:0:0,307 - endDate: Fri Dec 02 12:30:58 CET 2005
[EDT] 0:0:2,227 - Exception: java.lang.ArrayIndexOutOfBoundsException - Index out of bounds
java.lang.ArrayIndexOutOfBoundsException: Index out of bounds
at com.codename1.ui.spinner.SpinnerNode.setSelectedIndex(SpinnerNode.java:284)
at com.codename1.ui.spinner.SpinnerNode.rebuildChildren(SpinnerNode.java:213)
at com.codename1.ui.spinner.SpinnerNode.setListModel(SpinnerNode.java:227)
at com.codename1.ui.spinner.Spinner3D.setModel(Spinner3D.java:207)
at com.codename1.ui.spinner.DateSpinner3D.setEndYear(DateSpinner3D.java:340)
at com.codename1.ui.spinner.DateSpinner3D.setDateRange(DateSpinner3D.java:216)
at com.codename1.ui.spinner.Picker$1.createDatePicker3D(Picker.java:490)
at com.codename1.ui.spinner.Picker$1.showInteractionDialog(Picker.java:605)
at com.codename1.ui.spinner.Picker$1.actionPerformed(Picker.java:303)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
at com.codename1.ui.Button.fireActionEvent(Button.java:570)
at com.codename1.ui.Button.released(Button.java:604)
at com.codename1.ui.Button.pointerReleased(Button.java:708)
at com.codename1.ui.Form.pointerReleased(Form.java:3339)
at com.codename1.ui.Component.pointerReleased(Component.java:4528)
at com.codename1.ui.Display.handleEvent(Display.java:2079)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1051)
at com.codename1.ui.Display.mainEDTLoop(Display.java:969)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
Tested in the Simulator.
This is a bug in Picker I asked a user in the past to submit an issue on this but it seems that he didn't. It seems the default behavior of picker is to show 1970 as the oldest date. This is obviously problematic and should be addressed.
console.log("pre : "+vm.dailyCheckIn);
console.log(vm.temp_date.setHours(0,0,0,0));
console.log("next : "+vm.dailyCheckIn);
can someone help me with this code.
Result:
before temp variable changed (original date value)
pre : Mon Oct 29 2018 16:37:24 GMT+0530 (India Standard Time)
after temp variable changed (original date value)
next : Mon Oct 29 2018 00:00:00 GMT+0530 (India Standard Time)
It seems like that you have used the same date object in the temporary and in the actual variable. You have to create a new date object for the temporary variable.
e.g
var date = new Date();
var vm = {
dailyCheckIn: date,
temp_date: new Date(date) //Create a new date object
};
console.log("pre : "+vm.dailyCheckIn);
console.log(vm.temp_date.setHours(0,0,0,0));
console.log("next : "+vm.dailyCheckIn);
I hope it will help to you.
I am getting date format like this /Date(1495111091673)/.I have created one custom filter to change date format.
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1);
}
})
This filter returns date like this.
Thu May 18 2017 18:08:11 GMT+0530 (India Standard Time)
But I want it as standard format like dd/MM/yyyy so I have edited my filter code like this:
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1, 'dd MMMM # HH:mm:ss');
}
})
Is it correct?
This filter returns date like this
Thu May 18 2017 18:08:11 GMT+0530 (India Standard Time)
No it doesn't, that's just how your console (or whatever) is choosing to display the Date instance (via Date.prototype.toString()).
I'd just use AngularJS's date filter ~ https://docs.angularjs.org/api/ng/filter/date.
For example (where dateFormat is your "/Date(1495111091673)/" formatted string)
{{dateFormat | jsonDate | date : 'shortDate'}}
Or in JS
let parsed = $filter('jsonDate')(dateFormat)
let dateString = $filter('date')(parsed, 'shortDate')
or via DI
.controller('controllerName', ['dateFilter', 'jsonDateFilter',
function(dateFilter, jsonDateFilter) {
let dateString = dateFilter(jsonDateFilter(dateFormat), 'shortDate')
}])
I am fetching the date from my Django backend which comes like this: 2016-03-31
In my angular controller I want to compare it with today's date and if they are similar I want to deactivate a button.
I tried new Date() which gives me something like Thu Mar 31 2016 08:59:01 GMT+0200 (W. Europe Daylight Time)
How can these two dates be compared to achieve my goal?
ehhhhh... i think currently we could only do a manual formatting
see this one: How to format a JavaScript date
For your reference, below is what i did though:
$scope.formatDate = function(date){
var newDate = new Date(date);
var year = newDate.getFullYear();
var month = (newDate.getMonth() + 1).toString(); //add 1 as Jan is '0'
var day = newDate.getDate().toString();
month = month.length > 1? month: '0'+month;
day = day.length > 1? day: '0'+day;
$scope.date = day + '/' + month + '/' + year;
}
If you want to compare the date with now you can do the following:
var now = new Date().getTime();
var compareDate = new Date('2016-03-31').getTime();
if(now > compareDate){
//greater
}else if(now < compareDate){
//less
}else{
//equal
}
Just add what you need to the scope and then you could do something like:
ng-disabled="compareDate === today"
I have a Google Sheets spreadsheet. In Column B, I have a list of strings that are either dates or ranges of dates in the format month/date. For example:
7/26
7/27-7/31
8/1
8/2
8/3-8/5
I want to create an array with the first date on the left and the second date (if any) on the right. If there's no second date, it can be left blank. This is what I want:
[7/26,]
[7/27,7/31]
[8/1,]
[8/2,]
[8/3,8/5]
I've tried:
var r = 'B'
var dateString = sheet.getRange(dateColumns[r] + '1:' + dateColumns[r] + lastRow.toString()).getValues();
var dateArr = Utilities.parseCsv(dateString, '-');
But that just keeps concatenating all values. Also if it's possible to put the output in a date format that would be great too.
This was a funny exercise to play with...
Here is a code that does what you want :
function test(){
convertToDateArray('7/26,7/27-7/31,8/1,8/2,8/3-8/5');
}
function convertToDateArray(inputString){
if(typeof(inputString)=='string'){inputString=inputString.split(',')}; // if input is a string then split it into an array using comma as separator
var data = [];
var datesArray = [];
for(var n in inputString){
if(inputString[n].indexOf('-')==-1){inputString[n]+='-'};// if only 1 field add an empty one
data.push(inputString[n].split('-'));// make it an array
}
Logger.log(data);//check
for(var n in data){
var temp = [];
for(var c in data[n]){
Logger.log('data[n][c] = '+ data[n][c]);
var date = data[n][c]!=''? new Date(2014,Number(data[n][c].split('/')[0])-1,Number(data[n][c].split('/')[1]),0,0,0,0) : '';// create date objects with right values
Logger.log('date = '+date);//check
temp.push(date);
}
datesArray.push(temp);//store output data in an array of arrays, ready to setValues in a SS
}
Logger.log(datesArray);
var sh = SpreadsheetApp.getActive().getActiveSheet();
sh.getRange(1,1,datesArray.length,datesArray[0].length).setValues(datesArray);
}
Logger result for datesArray :
[[Sat Jul 26 00:00:00 GMT+02:00 2014, ], [Sun Jul 27 00:00:00 GMT+02:00 2014, Thu Jul 31 00:00:00 GMT+02:00 2014], [Fri Aug 01 00:00:00 GMT+02:00 2014, ], [Sat Aug 02 00:00:00 GMT+02:00 2014, ], [Sun Aug 03 00:00:00 GMT+02:00 2014, Tue Aug 05 00:00:00 GMT+02:00 2014]]