how to find time difference in titanium - time

I am new to titanium.
I want to find time difference in titanium. Ex 12.00 AM- 12.00 PM should give me 12 hours.
But I'm not able to get how to find it in titanium.
I'm trying
function calculatetime(chkintime,chkouttime)
{
var difference = chkintime - chkouttime;
Ti.API.info(':'+difference);
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
Ti.API.info(':'+hoursDifference);
Ti.API.info(':'+minutesDifference);
var time=hoursDifference+':'+minutesDifference;
return time;
}
It sometimes gives correct answer while sometimes negative values.
here chkintime and chkouttime values are in miliseconds e.g. 1355495784321

It's no different from finding a time difference in JavaScript. (In fact, it is finding a time difference in JavaScript.)
Check time difference in Javascript
Past that, a nice way to calculate the number of days between X and Y is to find out the MS difference, then add that time to a set date, like January 1st, 2000. Then you can really easily pull the number of years, months, days, hours, minutes, and seconds. There will be some inaccuracy caused by leap years, but if you're dealing with small period, it doesn't matter at all.
var start = new Date('February, 22, 2011 2:00 PM');
var end = new Date('February, 22, 2011 4:00 PM');
var ms = end - start;
var niceDate = new Date(new Date('January 1, 2000').getTime() + ms);
var years = niceDate.getFullYear() - 2000;
var months = niceDate.getMonth();
var days = niceDate.getDate();
var hours = niceDate.getHours();
var minutes = niceDate.getMinutes();
var seconds = niceDate.getSeconds();
alert(years + ' years,\n'
+ months + ' months,\n'
+ days + ' days,\n'
+ hours + ' hours,\n'
+ minutes + ' min,\n'
+ seconds + ' sec');

Related

DHTMLX Scheduler custom Timeline with AM/PM days

I use the DHTMLX Scheduler in Timeline view for a project and the working hours are from 7AM to 5PM. I was able to make an AM/PM view per day but the first_hour and last_hour configs are not respected in the view. The screenshot shows more. The first event in the morning should be displayed far more left as it starts at 7:30Am.
My config:
scheduler.locale.labels.timeline_tab = "Timeline";
scheduler.locale.labels.section_custom = "Section";
scheduler.config.dblclick_create = false;
scheduler.config.edit_on_create = false;
scheduler.config.details_on_dblclick = false;
scheduler.config.details_on_create = false;
scheduler.config.start_on_monday = true;
scheduler.config.first_hour = 7;
scheduler.config.last_hour = 17;
scheduler.config.full_day = true;
scheduler.config.mark_now = false;
scheduler.config.drag_move = false;
scheduler.config.drag_resize = false;
//===============
//Configuration
//===============
scheduler.createTimelineView({
name: "timeline",
x_unit: "hour",
x_date: "%A",
x_step: 12,
x_size: 14,
x_start: 0,
x_length: 14,
y_unit: scheduler.serverList('teams'), // sections and events from same ajax call
y_property: "team_id",
render: "bar",
section_autoheight: false,
dy: 30,
dx: 100,
// first_hour: 7,
// last_hour: 17,
second_scale: {
x_unit: "day", // unit which should be used for second scale
x_date: "%D %j %M"
}
});
Any help will be appreciated.
EDIT:
After update based on the answer here the result:
first_hour, last_hour configs applied for Y-axis in Day, Week, Units views (check Documentation).
Currently, to hide hours at the beginning and end of the day in the Timeline view, you should have only 1 X-axis like in this sample. I.e. visible part of the event is from 10 to 18 because of first_hour, end_hour properties of createTimelineView method. Check the screenshot.
There is also ignore_[viewName] function which can be used to hide interval that equal to min step of the scale (12 hours in your case).
This could help solve your issue if set min step = 6 (hours) and disable 6-7 AM and 5-6 PM by addMarkedTimespan. I tried to create a snippet for you http://snippet.dhtmlx.com/46ba545ad , but found out that the second part of this condition if(date.getHours() < 6 || date.getHours() > 17){ is not working correctly. The event can be created until 6 PM. We will fix it ASAP, but now I can't specify the exact time. I suggest you use the first way with 1 scale to solve the issue.

Solution time in CPLEX

I want to find the solution time of my model in CPLEX and I used the following code:
float temp;
execute{
var before = new Date();
temp = before.getTime();
}
// solve the model
execute{
var after = new Date();
writeln("solving time ~= ",after.getTime()-temp);
}
But the result is : 1.5592e+ 12, which is a huge number. So, do you know how can I reach to the the solution time in second or millisecond?
This is more of a pure javascript question rather than something related to CPLEX. The number you're getting is in milliseconds, but you can convert that into seconds, minutes, etc. using the techniques described at stackoverflow.com/questions/41632942. For example:
var timeDiff = after.getTime() - temp;
// Convert from milliseconds to seconds.
timeDiff /= 1000;
// Display diff in seconds.
writeln("solving time ~= ", timeDiff);

Specific Timezone when using Google Script API for Spreadsheet

I'm using Google Script API for filling a spreadsheet from a From Submission. When filling Timestamp it always gets me a different Timezone.
How can I set it to my specific Timezone EET GMT+2?
Here an example of my code:
var row = [ new Date()];
How about this sample? In this sample, the converted date can be retrieved by giving the GMT offset. For example, GMT+2 was given to the following sample.
var gap = 2; // GMT+2
var result = new Date(Date.now() + (new Date().getTimezoneOffset() + gap * 60) * 60 * 1000);
var row = [result];
Flow :
Date.now() is the unix time. The unit is milliseconds.
getTimezoneOffset() returns the time zone difference, in minutes, from current locale (host system settings) to UTC.
The unit of (new Date().getTimezoneOffset() + gap * 60) is minutes.
The unit of milliseconds is obtained by multiplying this by 60 * 1000.
By new Date(Date.now() + (new Date().getTimezoneOffset() + gap * 60) * 60 * 1000), the unix time is converted to the date object.
Reference :
getTimezoneOffset()
If this was not what you want, I'm sorry.

Swift 2 get minutes and seconds from double

I am currently storing time in seconds (for example 70.149 seconds). How would I easily get the minutes and seconds? What I mean is 70.149 seconds is 1min and 10sec. How would I be able to do this easily in swift?
Here is an example of what I want to do.
let time:Double = 70.149 //This can be any value
let mins:Int = time.minutes //In this case it would be 1
let secs:Int = time.seconds //And this would be 10
How would I do this using Swift 2 OS X (not iOS)
Something like this:
let temp:Int = Int(time + 0.5) // Rounding
let mins:Int = temp / 60
let secs:Int = temp % 60
This would be a relatively simple solution. It's worth noting this would truncate partial seconds. You'd have to use floating point math on the third line and call trunc()/ceil()/floor() on the result before conversion to an Int if you wanted control over that.
let time:Double = 70.149 //This can be any value
let mins:Int = Int(time) / 60 //In this case it would be 1
let secs:Int = Int(time - Double(mins * 60)) //And this would be 10
Swift 5
The date components formatter has lots of advantages when displaying human readable text to a user. This function will take a double.
func humanReadable(time:Double) -> String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .full
if let str = formatter.string(from: time) {
return str
}
return "" // empty string if number fails
}
// will output '1 minute, 10 seconds' for 70.149

How to set time to a variable without the date

I am trying to set a specific time to two variables but can't seem to correctly format the syntax. I want to be able to set Shift 1 and Shift 2 off of certain times indicated below.
I want to only be able to use these times in an IF statement so that a radio button can be checked by default. Day Shift button and a Night Shift button. If the current time is in between shift 1, then day shift button is checked.
Date.prototype.currentTime = function(){
return ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ this.getMinutes()+((this.getHours()>12)?('PM'):'AM'); };
var d1= new Date();
var d2 = new Date();
d1.setHours(7);
d1.setMinutes(10);
d2.setHours(19);
d2.setMinutes(10);
alert(d1.currentTime());
alert(d2.currentTime());
Thanks Any help is appreciated.
You do not need to compare Date objects for your use,
just compare the hours as integers to the integer from now.getHours():
var now= new Date().getHours();
if(now>6 && now<19){
//check day shift button;
}
// else check niteshift button
You may try this:
http://jsfiddle.net/apq59j9u/
Date.prototype.currentTime = function(){
return ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ this.getMinutes()+((this.getHours()>12)?('PM'):'AM'); };
var d1= new Date();
var d2 = new Date();
d1.setHours(7);
d1.setMinutes(10);
d2.setHours(19);
d2.setMinutes(10);
alert(d1.currentTime());
alert(d2.currentTime());
Getting the Data
There are a number of different ways to do this. First here is a way to get the data from the date object:
var d = new Date();
var n = d.toTimeString();
Unfortunately, this will output something like "12:30:30 GMT-0500 (Eastern Standard Time)"
You can also try the getHours() and getMinutes functions to get the hours and minutes in your current timezone.
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
Setting the Data
This is pretty easy to do, just as getting the data from a date object. Use the following code to set the time to what you would like. Replace the numbers where you see 11 to conform to your needs.
NOTE: This time is in military style hours! 1 = 1am, 13 = 1pm, ect.
var d = new Date();
d.setHours(11);
d.setMinutes(11);
d.setSeconds(11);
Result: 11:11:11

Resources