How to detect changes with Date objects in Angular2? - javascript

Date objects that are modified using setDate method arent getting updated in template.
In template:
<p>{{date | date:'mediumDate'}}</p>
In component:
nextDay(){
this.date.setDate(this.date.getDate()+1);
}
But when I call nextDay function, the template isnt updated with the new value.
The only way I could get the change detection working was doing this:
nextDay(){
var tomorrow = new Date();
tomorrow.setDate(this.date.getDate()+1);
this.date = tomorrow;
}
Are there a better way to accomplish this same task?

I think that is the right way, to change the reference of the date variable. From the docs here we have:
The default change detection algorithm looks for differences by comparing bound-property values by reference across change detection runs.
So if the date reference remains the same, nothing will happen. You need a new Date reference and that's why the second version of nextDay() works.
If you remove the formatting pipe you will see that still only the second version of nextDay() works.

Related

I override api_date of themplates in DHTMLX but it is not used?

In dhtmlx gantt, you can format input date (from string or any type) into javascript date. This date is used to draw chart. Base on the dhtmlx document, you can replace converter (api_date) by custom function:
I override the function as follow:
gantt.templates.api_date = function(date){
throw "It is called";
};
but, it never is called.
api_date template and config are no longer used. We will update the information in the documentation. Please use xml_date as hadi.mansouri suggested.
I don`t know why this function is not called. I also confuse for this, because official documents of DHMTLX Gantt (as you mentioned, api_date) say it should works.
However I found that if you override xml_date it will work as you want. Although it is named xml_date but it works for json data also.
So could use following snippet:
gantt.templates.xml_date = function(date){
// Your customized code.
// return a Date object.
};

momentjs override the default global date for today

I'd like to set a default time for momentjs. For instance the default behavior is:
moment().format('YYYY-MM-DD') // returns current date
What I'd like to do is to override the current date to some other date, i.e. 2017-03-01, so whenever I do
moment().format('YYYY-MM-DD')
>> "2017-07-31"
The Moment.js code calls new Date() to initialize itself when the constructor is called without arguments (technically, it calls new Date(Date.now()), but the result is the same). You have to pass something to get a specific date.
Of course, you could alter your local copy of the Moment.js library, but this is not recommended. You would have to keep it up-to-date with later releases of the libraries. And causing moment() to return anything other than the current date would cause those looking back at your code to wonder what's going on.
Upon further investigation, it seems Moment.js does allow you to overwrite the implementation of moment.now() which tells the rest of the library what time it is. See this article on the Moment.js website for more. There's an example there:
moment.now = function () {
return +new Date();
}
Which would be easy to alter for your needs:
moment.now = function () {
return +new Date(2017, 2, 1); // March 1st, 2017
}
I would strongly suggest using this technique sparingly (if at all) for the reasons given in the second paragraph above.

Comparing to current date time

I'm trying to mark up my first page with Ractive. I need to have an if expression that compares a momentjs property from my data object (EndDateTime) to the current date time. Here is what I tried:
{{#if EndDateTime.isAfter(moment()) }}
It doesn't work. If I change it to EndDateTime.isAfter(StartDateTime) (where StartDateTime is another moment object) it works.
Could you please help me understand why comparison to moment() doesn' work and how can I compare to the current date time?
moment needs to be defined in your data if it isn't already:
data: {
moment: moment,
... //other data
}
martypdx's answer probably explains why your current code doesn't work, but you can also take advantage of the fact that the isAfter function will default to the current time if you pass no arguments.
In other words, the code you gave is equivalent to:
{{#if EndDateTime.isAfter() }}
This is in the documentation here.

Save the actual date in a variable (only date, no time)

I want to save the actual date in a variable. only the date, no time
var a = #Date(#Now());
datasource.replaceItemValue("variable", a)`
And
var a = #Date(#Now());
var b = new Date(a.getYear(), a.getMonth(), a.getDay());
datasource.replaceItemValue("variable", b)
Are returning 28.10.14 00:00
var dt:NotesDateTime = #Date(#Now());
datasource.replaceItemValue("variable", dt.getDateOnly());
Is throwing me an error
Isn't there a simple way to get only the actual date without the time?
Use setAnyTime() metohd of NotesDateTime class to remove time component.
If you want to save only the date use a textfield and convert the text to date, if you need it in your code
#Now uses a java.util.Date, which includes time portions. .getDateOnly() is probably throwing an error because that returns a String.
The underlying session.createDateTime() method accepts either text, a java.util.Date or a java.util.Calendar. I think all of them need to include a time element.
If you're only storing the value for reference, I'd agree with brso05 to not worry.
If you're ever likely to use #Adjust (or an equivalent), then not worrying about the time is a recipe for disaster, because every time you try to adjust, you need to remember to take into account Daylight Savings Time.
One option is to set the time to 12:00 midday. That means DST will not be a problem.
java.sql.Date is specifically designed to only include the Date portion, without a time element. Jesse Gallagher talks about java.sql.Date in the context of his frostillic.us framework https://frostillic.us/f.nsf/posts/32A63DD640D868D885257D18006659A9 and he was the one I found out about java.sql.Date from. I'm not sure how he stores those values though.
I'm not sure if the OpenNTF Domino API will allow you to just pass a java.sql.Date to a field and so store just the date portion.

BIRT Using Date parameter in script

I am using BIRT and am trying to modify the text on a chart based on an end date report parameter, which I've assigned a default value and of course, Date datatype. SQL queries also use this parameter. To test passing of value, I write the below script.
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var endDate = new Date(params["rptEndDate"].value);
}
However, when I try to preview the chart, it is blank, indicating that there is something wrong with my script. When I comment out the var endDate line, it renders as normal. I've tried other ways to get the value like:
var endDate = new Date();
Which works, but when I try to assign the parameter value:
endDate = params["rptEndDate"].value;
It fails again. Removing the .value part also fails. Also fails when I try to use reportContext to get the parameter value. According to other sources my script should be correct. Is there anything I am missing?
This is due to the specifity of the chart engine: "params" object is not made available in chart scripts, use "getParameterValue" instead. It will work like this:
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var endDate = new Date(getParameterValue("rptEndDate"));
}
The Previewer tends to offer less then optimally formated or fully functional design. Open the report in the format/tool that will be used in the final delivery for true testing durring design. Personally I have Apache on my test server and do all of my preview testing as a web page.

Categories

Resources