<h1>TIME: <%= Time.now.strftime("%H:%M:%S") %></h1>
In the template, my time is stopped. I want to animate it time like a digital watch.
The time is changed when I refresh a page.
For example, I'm going to index page and now the time shows: 12:30:50 (HH:MM:SS). I refresh again and the time shows: 12:31:10.
I want to show my seconds like this: 50 51 52 53 54.....
function displayTime() {
var timer=document.getElementById('timer');
var currentdate = new Date();
var hours = currentdate.getHours();
var minutes = currentdate.getMinutes();
var seconds = currentdate.getSeconds();
timer.innerHTML = hours + ':' + ("0" + minutes).slice(-2) + ':' + ("0" + seconds).slice(-2);
};
setInterval(displayTime, 1000);
<h1>Time :
<p id="timer">
<%= Time.now.strftime("%H:%M:%S") %>
</p>
</h1>
First, let's create a function that displays the current date. For this, we just have to find the p tag we are interested in (using getElementById), and put use innerHTML to write in it. That's the function displayTime.
Then, we want to run this function every second, so we use : setInterval.
Note I keep <%= Time.now.strftime("%H:%M:%S") %> so that if javascript is not enabled there is still a date. If that's not the intended behavior then you can replace it.
Related
I have a google sheet populated by a google form input with 1 column that has a start date, the 2nd has the start time (the time is a text input from a drop down menu in a time format (but it is text) - 09:00 - 09:30 - 10:00 ..etc) I'm trying to join the date and time input to create a calendar event with the end date the same day as the start day and end time 30 minutes after. Any help would be appreciated!
I have searched and found that the text string for time can be converted by removing the ' sign infront of the text time input by using:-
var withoutQuote = e.values[1].substring(1);
My current script for createEvent:-
var options = { description: namedValues.Description[0],
location: namedValues.Location[0],
guests:"info#domain.com"};
var cEvent = CalendarApp.getCalendarsByName("TEST")[0].createEvent(
namedValues.Name[0],
new Date(namedValues.Starts),
new Date(namedValues.Ends),
options)
}
I would like to create new date based on input date (namedValues.Starts) + concatenate start time (namedValues.Stime) and new date based on (namedValues.Starts) + concatenate start time (namedValues.Stime) + 30 minutes
Any help would be appreciated!
Ok, I was able to get the concatenates namedValues part right herewith edit code:-
function createEvent_ (namedValues) {
var options = { description: namedValues.Description[0],
location: namedValues.Location[0],
guests:"johan#inprint.co.za"};
var cEvent = CalendarApp.getCalendarsByName("TEST")[0].createEvent(
namedValues.Name[0],
new Date(namedValues.Starts + " " + namedValues.Stime),
new Date(namedValues.Ends + " " + namedValues.Etime),
options)
}
What I need now is the second part - I would like to create a new end date/time - based on new Date(namedValues.Starts + " " + namedValues.Stime) + 30 minutes
I am trying to make a custom publish date, because the user wants to use that as the publishing date and for sorting. The date will also be displayed on the page.
Here is what I want:
The user can input a date
The date can be empty (meaning it will be published now)
It has to use that date for sorting
The date has to be set to UTC time, so it's equal for everyone in the world
I am desperate and I cannot figure out how to do this.
Here is what I have tried so far: I found a neat little plugin, which displays the user's current UTC time next to the input field, so the person knows their UTC time. I modified that to always enter the current date in the input field:
var timer = setInterval(function () {
var date = $(".custom-date").val();
if (date === "") {
$(".custom-date").focus();
$(".custom-date").click();
$(".custom-date").trigger("click");
//the date has now been set on the input field
} else if (date !== "") {
var newDate = new Date(date);
var stringDate = newDate.getFullYear() + "-" + ('0' + (newDate.getMonth() + 1)).slice(-2) + "-" + ('0' + (newDate.getDate() - 1)).slice(-2) + " " + ('0' + (newDate.getHours() - offset)).slice(-2) + ":" + ('0' + newDate.getMinutes()).slice(-2) + ":" + ('0' + newDate.getSeconds()).slice(-2);
$(".custom-date").val(stringDate);
angular.element(".custom-date").scope().$apply(function () {
angular.element(".custom-date").scope().datetimePickerValue = stringDate;
});
clearInterval(timer);
}
}, 1000);
Yes, this looks like a lot... and no, it does not work. I do the focus/click/trigger on the element, because that will automatically set the time to be the user's local time. I then turn that into UTC time (offset is the UTC time offset). Then I apply the date to the element's scope and the value gets updated both in $scope and in the view (I can actually see it).
However, when I hit save and publish, the date gets reset (it's empty in the database). It's only when I physically click on the input field and select a new date it will actually update it. I like this method, as I am in 100% control of it, so is it possible? It would seem like setting the new date on the scope doesn't trigger the actual "new date has been selected".
Alternatively I have my Razor code here:
//selection is all my elements/nodes
selection.OrderByDescending(x => x.GetProperty("publishDate") != null).ThenByDescending(x => x.GetPropertyValue("publishDate")).Where(x => x.GetPropertyValue<DateTime>("publishDate") < DateTime.UtcNow);
So apparently before the Angular event is triggered, I need to, at least, call these:
$(".custom-date").trigger("click");
$(".custom-date").change();
So I did that right after I set my new date and now it works.
My MVC is Struts 2
My requirement is to clear some error message in the leading page AFTER the back button is clicked in the browser.
The sequence is:
Page A --> Page B --> Page C
The Page B can be navigated from Page A or from Page C (by clicking the back button). If Page B has an error message, and back button from Page C is clicked, this error message has to be removed. But I have no way of determining how the user navigated to Page B (considering there are 2 ways as mentioned above).
Searched everywhere for a solution. Found out that window.history is not a reliable way to parse the previous page.
Also, the window.location is not changing because of the Struts 2 quirks with tiles.
The frustrating thing is browser back button does not hit the server. Neither does it process JSP or struts tag. Luckily, though, it runs the javascript.
So I came up with a custom solution that uses server and client timestamps. It seems to work in all browsers.
I would like to know if there is an easier way.
<%
if (session.getAttribute("myTime") != null) {
//server sends the time in millis on the first invocation
Long tmStamp = Long.parseLong((String) session.getAttribute("myTime"));
// out.println(tmStamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date javaDt = new Date();
javaDt.setTime(tmStamp);
// out.println(" " + sdf.format(javaDt));
Date clientDt = new Date();
Long clientMillis = clientDt.getTime();
Long timeDiff = clientMillis -tmStamp;
%>
<script>
var currDt = new Date().getTime();
var dt = new Date(<%= tmStamp%>);
dt.setTime(<%=tmStamp%>);
var jsDt = new Date();
jsDt.setTime(currDt);
//alert( "<%= tmStamp %>" + " javascript time=" + (currDt - dt.getTime()) + ' timeDiff=' + (1 * <%= timeDiff%>) + " " + jsDt + " " + dt);
//we expect the server to repond within 5 seconds
if (currDt > (<%= tmStamp %> + (1*<%= timeDiff %>) + 5*1000)) {
//alert("referer is summary" )
clearError();
} else {
//alert("referer is not summary");
<s:if test="flag == 'none'">
$('<tr><td align="center"> <font color="red" style="font-size:14px;font-family:"sans-serif, Arial, Helvetica";height:20px;width:180px; font-weight:bold;" ><blink>No Result found for the given Search Criteria</blink></font></td></tr>')
.insertAfter("#lastRow");
$('<tr><td align="center"> <font style="font-size:14px;font-family:"sans-serif, Arial, Helvetica";height:20px;width:180px; font-weight:bold;" ><blink>No Result found for the given Search Criteria</blink></font></td></tr>')
.insertBefore("#firstRow");
</s:if>
}
</script>
<% } else {
out.println("No session attribute for myTime");
}
%>
How do I call a JavaScript function from html.erb
in application.js
function displayClock() {
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var amOrPm = 'AM';
if (hours > 11) amOrPm = 'PM';
if (hours > 12) hours = hours - 12;
if (hours == 0) hours = 12;
if (minutes <= 9) minutes = '0' + minutes;
if (seconds <= 9) seconds = '0' + seconds;
dispTime = '<b>'+hours + ':' + minutes + ':' + seconds + ' ' + amOrPm+'</b>';
document.getElementById('time').innerHTML = dispTime;
setTimeout('displayClock()', 1000);
}
and in home.html.erb
displayClock();
but it's not calling function!
You probably need to use proper tag to indicate that you're writing JS code:
<script type="text/javascript">
displayClock();
</script>
Plus, since you probably use jQuery (it's default in Rails), you may want to make sure the function is called after whole document is loaded:
<script type="text/javascript">
$(document).load(function(){
displayClock();
});
</script>
I'd also advice you not to write your JS in application.js and use this file only as a manifest file, which includes your other scripts through Rails asset pipeline.
Also you can take advantage of the javascript helper javascript_tag in your html.erb file
<%= javascript_tag "displayClock()" %>
I have a form that users are allowed to enter time values in (say, hours spent performing some task). A business requirement is that the time be entered in either hh:mm format, or decimal format. In either case, there could potentially be quite a bit of client side javascript for display "helping"--showing totals, validating against other input, etc.
So, for instance, one user might enter "8:30" for Eight Hours Thirty Minutes, while another might enter "8.5" to mean the same thing. I'm looking for something that would let me keep the form/validation duplication to a minimum.
What are the best ways to go about this with the model and the view?
The regular expression to allow both formats wouldn't be that complicated. I would perform that simple validation client-side via javascript. Beyond that, you may want to add some business validation (at the business object level) for this.
I used jQuery to create a slider that would change the time in the input box in the right format.
In my View File, Create.aspx, put the following jquery function somewhere in the beginning of the body.
<script>
$(function () {
$("#slider").slider({
value: 9,
min: 0,
max: 1440,
step: 15,
slide: function (event, ui) {
var hours = Math.floor(ui.value / 60);
var minutes = ui.value - (hours * 60);
var ampm = "AM";
if (hours == 12) { ampm = "PM"; }
else if (hours == 0) { hours = 12; ampm = "AM"; }
else if (hours > 12) { hours = hours - 12; ampm = "PM"; }
if (hours < 10) hours = '0' + hours;
if (minutes < 10) minutes = '0' + minutes;
$("#work_StartTime").val(hours + ':' + minutes + ' ' + ampm);
}
});
});
</script>
then down in the body of the same page put a div near the textbox for time input. A slider will show up at that div
<div class="editor-field">
<%: Html.EditorFor(model => model.work.StartTime)%>
<div id="slider"></div>
<%: Html.ValidationMessageFor(model => model.work.StartTime)%>
</div>
this will give you a slider. In the above javascript code change the step:15, makes increments to be 15 minutes.
This is obviously a client side validation. A server side validation should also be implemented of course.