Not able to update digital clock - javascript

I have to display respective time zone in digital format. The clock is displayed but it only updates after refreshing the page. Below is the code I used.
Below is my javascript code:
time: function() {
var zone = _.date.fleetTimeZone();
return moment().zone(zone).format('h:mm:ss a');
}.property('DS.session.last_fleet_interacted'),
Below is my handlebars:
<div class="time">{{time}}</div>

You'll need to turn time into a property, and update it each second, using setTimeout() or something. Then it'll work because of data binding (I assume Ember.js because tags).
Alternatively, just do a setTimeout() and re-render the time manually.

Related

How to use UNIX time in HTML Datetime attribute?

I'm working on a single page chat application, and when displaying the message sent by some user ,I want all the other users to see the same exact time the message was sent in and also the correct time regardless user's computer time or any other reason. As I understood UNIX time do the job ,and I want to use it in my app and put it inside datetime HTML attribute . Is there a way to do that in HTML or should I use JS for that?
Note: I think that this question isn't relevant to me because first I want to use the attribute datetime and second thing I want to use Unix time .
Any help is appreciated.Thank you.
Something like this?? You can set the dateTime attribute of the time tag with the date.toISOString function since it is one of the supported formats by the time tag.
var unixTime = 0; // fill in your epoc time here.
var isoTime = new Date(unixTime).toISOString()
timeEl.textContent = unixTime+' --- '+isoTime+' --- '+new Date(isoTime).toString();
timeEl.setAttribute('datetime', isoTime )
<time id="timeEl"></time>

Efficient way to run code based on time

I want to run come specific code after specific timing but with a efficient technique, Not with SetTimeout/SetInterval.
Like request to Database to check data on a after specific time dialy.
I want to query database at after 5:00 pm. So i write this code.
function checkVotingQuestionTime{
var currentDate = new Date();
if(currentDate .getHours =>5){
//go to database
}
}
But I have to write this code in setInterval(checkVotingQuestionTime, 60000);
Is there any better way to do this job? I guess current code is performance killer. register event etc
I would recommend using a 3rd party solution like
http://bunkat.github.io/later/
or
http://bunkat.github.io/schedule/
The first will overwrite setInterval() for you, so that you can use later schedules instead of providing a timeoffset in milliseconds:
https://bunkat.github.io/later/execute.html#set-interval

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.

updating a Date object on the client side after getting the correct Date from the server side

I have a UI that needs to have the UTC time updated all the time to be shown to the users.
my original code was this:
(function () {
$http.get('api/getdate').success(function (data) {
current = new Date(data.res + new Date().getTimezoneOffset()*60*1000);
updateDateTimer = setInterval(function(){
$http.get('api/getdate').success(function (data) {
current = new Date(data.res + new Date().getTimezoneOffset()*60*1000);
});
}, 30000);
});
})();
data.res has the correct timestamp i need. this way works fine because i can trust my server with the data that is sent back to the UI. then the only data that i need from the user is its offset from UTC and thus i am sure the date that will be displayed will be correct.
my problem is that a call to the server will be made every 30 seconds, and if i want my clock to change even more often then this can get really nasty.
i thought about making the call to the server only one time and then add 1 second with a timer to the Date object created but this is very not accurate and after a few minutes you can see that the clock is not synchronized any more with the real time.
is there any possibly to not make more calls to the server after the first time?
the problem is that i can't make sure the client won't change its local clock. it seems i need to some how deal with the Date object that is being created with the first .get call, but how?
thanks
Could you please clarify the question a bit? If the only thing you're trying to do is find/show the current UTC time, new Date().getTime() returns the millis since January 1, 1970 UTC so that time is already in UTC (2 calls of getTime() occurring simultaneously in New York and Los Angeles will show the same number). Then you have some UTC functions on the Date object which give you the UTC time, like getUTCHours(), getUTCMinutes and getUTCSeconds().

Coffeescript: Dynamically update time with moment js with coffeescript

I am having trouble dynamically seeing the time update with moment js. It shows the correct time but I have to refresh my browser to get the time update. I would like it to update it in real time. Like momentjs.com main page.
I've tried using setInterval and setTimeout but for some reason I get the below digits that don't even update.
Here's what I have so far code-wise. Pretty simple as far as moment goes and all I want is seconds to keep counting...
update = ->
time = moment().format('hh:mm:ss a')
clock = setInterval update, 1000
console.log(clock) //output: 53296
Any ideas are immensely appreciated.
You should put the output inside of the update method and everthing will work as expected.
The method setInterval won't return the result of the repeatedly called method but an identifier which can be used with clearInterval to stop the execution.
Just a small working example to print the time every seconds and stop after 10 seconds:
update = ->
console.log(moment().format('hh:mm:ss a'))
x = setInterval update, 1000;
setTimeout (-> clearInterval(x)), 10000
If you want to use that time as content of some DOM-Element you can use the following code inside your update function (assuming you have an element (e.g. div) with id "time"):
JQuery:
$("#time").text(moment().format('hh:mm:ss a'))
Plain JS:
document.getElementById("time").firstChild.data = moment().format('hh:mm:ss a')
Try this. If u are dont have to use meomentJS.
https://github.com/furkankaynak/countdown

Categories

Resources