Specific Timezone when using Google Script API for Spreadsheet - google-api

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.

Related

How do I get the offset for a time zone in ColdFusion

I have a need to get the offset (in hours) for any given time zone in Adobe ColdFusion. The idea is to pass in a time zone (America/Phoenix) and get back it's offset taking into account daylight savings.
Well after looking for what seemed forever, I realized CF doesn't have a way to do it. You need to delve into it's underbelly (JAVA) to get what you need. So, with a little help from a post by Ben Nadel on time zones, I figured it out and decided to pass on what I learned to a fellow dev traveler who may need it one day.
<cfscript>
/*
Author: Steve Holland (Avant Solutions Group)
Website: www.avantsolutionsgroup.com
License: https://opensource.org/licenses/MIT
*/
private struct function calcTZOffset(required string timeZoneID, boolean returnTimezones="false") {
// Init the timezome java class
var tz = createObject( "java", "java.util.TimeZone" );
// Get the timezone info
var tzInfo = tz.getTimeZone(
javaCast( "string", arguments.timeZoneID )
);
// Get the offset for the timezone
var tzOffset = tzInfo.getOffset(
javaCast("long", 0)
);
// Get the offset hours
var tzOffsetHours = tzOffset / 3600000;
//Check if the timezone is observing DST
var inDST = tzInfo.observesDaylightTime();
var tzOffsetHoursDST = inDST ? tzOffsetHours + 1 : tzOffsetHours;
//Return
var offset = {
inDST = inDST,
timeZone = arguments.timeZoneID,
offsetMillis = tzOffset,
offsetHours = tzOffsetHours,
dstOffsetHours = tzOffsetHoursDST,
timeZones = (arguments.returnTimezones)? tz.getAvailableIDs(): [] //Allow the user to return all the timezones (optional)
};
//writeOutput(tzOffset / 3600000);
return offset;
}
tzID = "America/Denver";
//tzID = "America/Phoenix";
//tzID = "America/Los_Angeles";
offset = calcTZOffset(tzID, false);
//Dump out the results
writeDump(var=offset);
</cfscript>
This should give you the UTC offset in hours. I know it works in Lucee but should in ACF also.
var timeZone = getTimeZoneInfo("America/Phoenix");
WriteDump(timeZone.utcHourOffset);

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);

Minus from slider value in AfterEffects

I'm making a timer. When the minutes value reaches 60, it has to decrease by 60 and increment the hour. It's tracking a time lapse; the minutes is currently the time of the computation divided by 6 : each frame is 10 seconds in real life.
This is the code I have so far:
effect("Mins")(1)+Math.floor(((time*10)/6))+effect("MinAdd")("Slider");
if(effect("Mins")("Slider").value > 60) {effect("Mins")("Slider") -60;}
Unfortunately, it doesn't work, and I don't know why.
I'm not quite sure what your setup is there, but usually property values are read by reading the .value property and set through setValue(), so in your case e.g.
effect("Mins")("Slider").setValue(effect("Mins")("Slider").value - 60);
Personally I'd probably use something more along the lines of:
var currentsec = (time*10)/6);
var minute = Math.floor(currentsec / 60);
var sec = currentsec % 60;
effect("Mins")("Slider").setValue(minute);
effect("Seconds")("Slider").setValue(sec);
though in your case of course for minutes and hours.

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 find time difference in titanium

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');

Resources