Adding DateTime attribute to SpineJS models - coffeescript

I am looking for the best way to handle Dates & Times on my SpineJS models. I am working on creating an event calendar of sorts. The first hurdle being to display a nice 6 week calendar. It must start on the last Sunday of the previous month, ending on the first Saturday of the next month.
I was using DateJs (http://www.datejs.com/) and handling some date things in the View template but wanted to move some of this to a model to cleanup said view.
The function I am working on is called 'firstDay'. This will find the date where we start our calendar (the last Sunday of the previous month)
firstDay: () ->
Date.today().set({month: #month}).moveToFirstDayOfMonth().moveToDayOfWeek(0, -1)
My attempted usage:
<% day = cal.firstDay %>
// initialize the table header, etc
<tbody>
<% while !day.equals(cal.lastDay): %>
// render each calendar tile/square
And here is the error:
Uncaught TypeError: Object function () {
return Date.today().set({
month: this.month
}).moveToFirstDayOfMonth().moveToDayOfWeek(0, -1);
} has no method 'equals'
So my Spine model appears to be unaware of DateJS... I guess that makes sense. It looks like the function itself is being returned, not the evaluation of the function... if that makes sense.
Any guidance here would be appreciated on the best way to incorporate dates and times into my models.
thanks

So my problem was quite simple... syntax. Being very new to coffescript and spinejs I guess I was looking at too many things at once and missed the obvious.
The proper way to call this function from a coffeescript/eco template is:
<% day = cal.firstDay() %>
I was missing the parenthesis on the function call. This mistake should be easy to recognize if I run across the error again in the future. In fact it should have been obvious in the first place... again, too many new things.

Related

Automatically set date in the front matter of a Jekyll page

When I create a post, in the front matter I have to insert layout, title, date etc. My concern is regarding date front matter. It is very inconvenient to insert date time and time offset in date field manually.
I'm not able to figure out how to do this automatically. I want the front matter date to update automatically with system time.
The date variable in front matter is an optional variable that you can set to be able to override the date that you set when you title your post according what Jekyll is looking for (i.e. 2019-12-14-post-title.md). It should only refer to the date the post was originally made, and not the current system date. Except for certain edge cases, you really shouldn't have to define date in the front matter.
You can use Liquid templating to post the current time to a page by passing the "now" keyword to date in your liquid template as follows:
This page was last updated at {{ "now" | date: "%Y-%m-%d %H:%M" }}.
to get the the output:
This page was last updated at 2019-12-14 17:48.
However this method has a big drawback for Jekyll usage, because it will refer to the current date when the page was last generated from the template and not when the page is being visited by the user.
The best way to access the current system time is likely to use javascript instead. This is fairly easy depending on your familiarity with the language, but essentially you can make a Date object and then output the info you are looking to display. Here is a quick snippet I made that shows a couple methods that act on the Date object to output different values such as the date, time, and year:
<!DOCTYPE html>
<html>
<body>
<h3>The toDateString() method converts a date to a date string:</h3>
<p id="demo1"></p>
<h3>The toLocaleTimeString() method outputs formatted time:</h3>
<p id="demo2"></p>
<h3>The getFullYear() method outputs the year:</h3>
<p id="demo3"></p>
<script>
// create date object
var d = new Date();
// use toDateString() to output formatted date
document.getElementById("demo1").innerHTML = d.toDateString();
// use toLocaleTimeString() to output formatted time
document.getElementById("demo2").innerHTML = d.toLocaleTimeString();
// use getFullYear() to output year
document.getElementById("demo3").innerHTML = d.getFullYear();
</script>
</body>
</html>
W3schools has a good rundown of the Date object and how to format output if you need more examples and reference on usage.
If you need to know how to use Javascript with Jekyll there is a helpful post on Jekyll Talk that will help.
First of all, you do not HAVE to insert 'layout, title, date etc'. You can use Front Matter defaults for them. This only makes sense for the layout as the title can or should be default, nor the date.
The date MUST be set in your filename and can be overridden in the Front Matter. Please note that this is ONLY true for the built-in collection posts. If you use a custom collection you do not need a date at all.
However, if you choose to use posts nevertheless and you want to add the date automatically, there is only one real option: Use a CMS that automates this input, like Forestry.io or CloudCannon.

Phoenix Datepicker to only show available dates

I am working on a simple booking system using the Phoenix framework. At the moment, I have a route for new bookings, displaying a date picker which takes the values from a "booking" model with the following attributes:
:time, Ecto.DateTime
:description, :string
belongs_to :user, Test.User
When I head to "bookings/new", my form is correctly displayed and I can pick whatever date I want. However, I am now trying to figure out how to remove unavailable dates from the picker.
As far as I can see, the form is taking the values from ecto's DateTime primitive type, however coming from an OOP background I cannot figure out what resource I need to update in order to remove unavailable dates. Do I have to create a custom type?
Thanks in advance
I am quite late to respond to this but I hope it will be able to help someone; you can use the builder option when using the date_select on forms.
<%= date_select f, :time, builder: fn b -> %>
<%= b.(:day, prompt: "Date") %> /
<%= b.(:month, prompt: "Month") %> /
<%= b.(:year, prompt: "Year") %>
<% end %>
That's how you can customize it if you need just the day, month and year. You can do the same with time too.
You can't block arbitrary dates in the default DateTime picker. You can restrict the range (e.g. limit the years to 1234 to 1243 or months to January to June), but since they're independent <select> inputs, you can't hide specific combinations. Two ways to solve this that I can think of are:
Do the validation on the server. You can use AJAX to do the validation as soon as the user selects, without a page refresh, if you want.
Use JavaScript to restrict the range, either blocking selecting a taken date using custom JS, or use an existing, full fledged DatePicker / Calendar library.

eonasdan datetimepicker century threshold

Any tips would be welcome: I need the datetimepicker to transform a date earlier than 1969 to that year in the 20th century. So if I enter 12-12-69 the datetimepicker transforms that into 12-12-1969. Entering 12-12-68 though is transformed into 12-12-2068. I assume this is done in moment.js, but I may be mistaken. If it is done in moment.js anyone have an idea where to look to change the threshhold for this automagic transformation? I'd rather change it in the library than use parseInputDate, but if anyone has an example on usage of parseInputDate, please share. I have found that the date already has been changed automagically as ddescribed above when I try and access the date-object via:
$('#datefrom').on('dp.change',function(e) { console.log(e.date); });
I need to access the date object B4 this happens. It works to set a onchange="dostuff()" inline on the input field itself, but that sort of feels wrong, as I want to handle this in one place in the datetimepicker and not in 50 places inline.

How can I semantically define within the <time> tag 'the present'

I am using time tag to define a time—seemed like the right approach ;-)
My problem however is the value I want to place within the time tag is NOW: the present. I get this validation error:
The text content of element time was not in the required format:
The literal did not satisfy the time-datetime format."
Looking at the spec, it doesn't seem possible to define 'NOW'. That's a nuisance. Any ideas on how to approach this?
You cannot. The time element in HTML5 is defined as markup for specific moments or periods or durations of time or for time offsets, not for all concepts related to time.
You can write e.g. <time datetime="2013-04-06T13:53">now</time>, thereby associating a fixed moment of time with the text content “now”. I don’t see how this could be useful, and the usefulness of the time element in general is questionable (it looks like markup for markup’s sake), but things like this are all you can do to “define ‘NOW’” with time.
I'm using this markup for now:
<time datetime="2013">Present</time>
No more errors, and it's symatic. It's just a shame there is no accepted variable for NOW.

Get date difference in jstl

I have a date and want to show difference of it with current time as --year--month--days--hours--minutes--seconds. How can I do this jstl? It is sure that the date will be greater than current datetime.
Using JSTL you could do some code gymnastics like:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:parseNumber
value="${(now.time - otherDate.time) / (1000*60*60*24) }"
integerOnly="true" /> day(s) passed between given dates.
But as code suggests, this gives overall difference and hardly could be a "calendar aware" way of doing it. I.e. You could not say: "3 years, 1 month and 2 days passed since otherDate".
Another example for this "days passed..." style, using a JSP tag and using "today/yesterday/days back" presentation:
<%--[...]--%>
<%#attribute name="otherDate" required="true" type="java.util.Date"%>
<jsp:useBean id="now" class="java.util.Date" scope="request"/>
<fmt:parseNumber
value="${ now.time / (1000*60*60*24) }"
integerOnly="true" var="nowDays" scope="request"/>
<fmt:parseNumber
value="${ otherDate.time / (1000*60*60*24) }"
integerOnly="true" var="otherDays" scope="page"/>
<c:set value="${nowDays - otherDays}" var="dateDiff"/>
<c:choose>
<c:when test="${dateDiff eq 0}">today</c:when>
<c:when test="${dateDiff eq 1}">yesterday</c:when>
<c:otherwise>${dateDiff} day(s) ago</c:otherwise>
<%--[...]--%>
Note:
In your software problem domain, if it makes sense to talk about days and months in a calendar way, probably you should have that expressed in your Domain Model. If not, at least you should benefit from using another lower software layer to provide this information (and for example using java.util.Calendar or Joda-Time APIs).
Not sure there are any built in ways of doing this with JSTL. you could write your own tag library or potentially use expression language (EL) like below.
${(dateObj) - (now.time)}
taken from Looking for JSTL Taglib calculate seconds between two dates
Mark I'm uncertain if this is possible using JSTL and one way would be to create your own custom tag to handle this as #olly_uk suggested. Me personally would not use any expression language on my JSP as this might also affect readability and not best practise.
You could also have this calculation/date difference when your page bean is constructed, that way avoiding any EL or a new tag. This also might have its limitations such as I'm not sure if the date you want to check the difference is entered by the user on field where you want to display the result instantly etc if you see what I mean.
Also another you could try depending on your scenario is using jQuery to calculate and display the difference, I thought I'll link this page anyway from SO.
How do I get the number of days between two dates in JavaScript?
JQuery Calculate Day Difference in 2 date textboxes
Hope this helps.