I use a primefaces extension timer with the following code :
<pe:timer
style="color:darkgrey;"
timeout="1000"
forward="false"
format="HH:mm:ss"/>
But I have a start value. The previous code start from the value 00:00:00, but I have a start time from a Java bean. I have a Date object, or date as long type (from 1970) which is the value from I want to start.
For example I got 1548434800083 or 17:47 25/01/2019 and I want to display the time between this date and now. So how I can set my start value with this date ? I got milliseconds but I can get seconds instead of.
The solution is the following, I have a Java bean, that contain a date in millisecond. I get it with the function getTime() from the Java class Date.
I display my counter, chronometer with the primefaces code :
<pe:timer
style="color:grey;"
forward="true"
formatFunction="return displayElapsedTime('#{synopticBean.longEnteredTime}');"/>
That code call a javascript function, that return a format counter/chronometer :
function displayElapsedTime(longEnteredTime){
var now = new Date();
var elapsedTime = now.getTime() - longEnteredTime;
var numHours = Math.floor(elapsedTime / 3600000);
var minutesAndSecondMS = elapsedTime - numHours;
var minutes = Math.floor((minutesAndSecondMS % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((minutesAndSecondMS % (1000 * 60)) / 1000);
var innerHtmlText = (('0' + (numHours)).slice(-2) + ":" +('0' + (minutes)).slice(-2) + ":" + ('0' + (seconds)).slice(-2));
return innerHtmlText;
}
It display a duration, the number of hours. I only count with hours, no day.
Best solution for your case is formatting. But sometime we need to set start value of primefaces timer:
Run javascript:
PF('timer').currentTimeout = 100;
Related
The question is how is it possible to get difference in days from before to today and knowing this difference is happened in the past?
Yes its possible to get difference in days from before to today, all you need is the dates. for the example this is some check in and check out date diff.
// Params in date
public static function getDateDifference($check_in, $check_out)
{
$check_in = strtotime($check_in);
$check_out = strtotime($check_out);
$date_difference = $check_out - $check_in;
$date_difference = round($date_difference / (60 * 60 * 24));
return $date_difference;
}
Currently I have a form with checkboxes and a table with several outcomes. This all works great with a Javascript. But in order to use this savely on a forum it is better to not use Javascript as these are used inside postings.
At the moment this form has several checkable options for calculating factors to decrease a standard milliseconds time, and output them in a result.
Now I want to rebuild it to not use Javascript. And that should works for above options. But those milliseconds have to show up in the end as hours : minutes : seconds.
Is this possible to achive without the use of the Java function? (and without php) So HTML or CSS only.
I already have looked in several things, like the pure CSS calculator etc, but those are all numbers only. I need a time convertion.
Currently I use this functions code
// function milliseconds to time
function msTime(s) {
var ms = s % 1000;
s = (s - ms) / 1000;
var secs = s % 60;
s = (s - secs) / 60;
var mins = s % 60;
var hrs = (s - mins) / 60;
mins = ("00" + mins).substr(-2);
secs = ("00" + secs).substr(-2);
return ' ' + hrs + 'h ' + ' ' + mins + 'm ' + ' ' + secs + 's';
}
It is not possible without Javascript. you can simplify the function as follows.
var secondsToTime = function(duration) {
var date = new Date(duration);
return "%hours:%minutes:%seconds"
.replace('%hours', date.getHours())
.replace('%minutes', date.getMinutes())
.replace('%seconds', date.getSeconds());
}
console.log(secondsToTime(1324339210310));
I'm trying to make a countdown timer, where I could easily set the endDate variable in html...
I can send strings to AS3 with flashvars="endDate=123", but how do I make it to be date (for example christmas)? - I need it to be date because then in AS3 I have something like endDate - todayDate = timeRemaining
Just pass date to flash vars using formats specified here and parse that string in AS3 using Date.parse() method.
In this example you get a countdown in days to Xmas.
var dateFromFlashVar : String = "2013/12/25 15:30:20 GMT+0300";
trace( new Date( new Date( Date.parse( dateFromFlashVar ) ).time - new Date().time ).getDate().toString() );
to transfrom a unix_timestamp date to as3 Date, you can do:
// unixTimeStamp is in seconds so we multiply by 1000 because flash Date take miliseconds
var date:Date = new Date( unixTimeStamp*1000 );
to get back unix timestamp of a date you can do:
// divide by 1000 to get seconds instead of miliseconds
var timeStamp:int = date.time / 1000;
i hope this is what you where searching for
I have two values coming in, a string in the form StartTime YYYY-MM-DD HH:MM:SS and a Duration in the form HH:MM:SS. from these two values I need to determine the endTime and put it in the same format as the StartTime.
I have tried to make something like
startTimeArray:Array = StartTime.split(/[ -:]/);
var date:Date = new Date(startTimeArray[0], startTimeArray[1]-1, startTimeArray[2], startTimeArray[3], startTimeArray[4], startTimeArray[5] );
but the split of course results in strings and not numbers and I can't figure how to convert all these sections of the time into numbers, and following that I do not see a way to apply math by adding the Duration Date object to the StartTime Date object if I am able to get them properly converted.
Am I going down a good path here? if so, how can I convert an array of strings to numbers and how can I add the duration to starttime? Thanks
const start:String = "2011-04-03 01:39:48";
const startArray:Array = start.split(/[: -]/g);
var startDate:Date = new Date(
startArray[0], startArray[1]-1, startArray[2],
startArray[3], startArray[4], startArray[5]
);
const duration:String = "02:10:10";
const durationArray:Array = duration.split(/:/g);
var durationDate:Date = new Date();
durationDate.setTime(
durationArray[0] * 3600000 + /* hour = 60m * 60s * 1000ms */
durationArray[1] * 60000 + /* minute = 60s * 1000ms */
durationArray[2] * 1000 /* second = 1000ms */
);
var finalDate:Date = new Date();
finalDate.setTime(startDate.time + durationDate.time);
trace(startDate);
trace(durationDate.time);
trace(finalDate);
Outputs:
Sun Apr 3 01:39:48 GMT-0300 2011
7810000
Sun Apr 3 03:49:58 GMT-0300 2011
You will want to split the date and time first and then work from there with the arrays.
var date_:String = StartTime.substring(0,StartTimet.indexOf(" "));
var time_:String = StartTime.substring(StartTime.indexOf(" ")+1, StartTime.length);
trace(date_); // YYYY-MM-DD
trace(time_); // HH:MM:SS
Then split your date into an array
var date_array:Array = StartTime.split("-");
Cast the elements of the array with Number
Then do the same for time but use ":" for the split.
Duration can be split the same way. Then just use normal addition after casting.
var d:Date = new Date(year,month,date,hour,minutes,seconds);
var d_added:Date = new Date(year,month,date,hour+duration_hour,
minutes+duration_minutes,seconds+duration_seconds);
Perhaps a stupid question, but i really dont know the answer :(
lets say i have a date object, how can i change its AM date to PM, or vice verca?
Thanks
seems like you can
var currentTime = new Date();
var hours:uint = currentTime.getHours();
then you can say, if hours is greater than or equal to 12, then subtract by 12, otherwise, add 12 to it, for example, by
public static const millisecondsPerHour:int = 1000 * 60 * 60;
var reversedAMPM = new Date(currentTime.getTime() + (12 * millisecondsPerHour));
Although Jian's version should work, here's an alternative.
Assuming var date:Date is initialized, you may literally change it:
date.hours += (date.hours > 12) ? -12:12;