Converting hours to HH:MM format - zapier

I need to insert the duration of a task into Pipedrive in the format HH:MM.
I have made a small Java script using code in zapier to find the difference between start og end time and thereby find number of hours or minutes that is the duration.
But I don't know how to convert this into HH:MM using JavaScript. Any suggestions?
Thanks
Steve

What is the unit of measurement for the duration that you have?
If it's in minutes, you can use the below code as reference to convert minutes into HH:MM format.
The duration_minutes variable refers to a variable that holds the initial duration that you calculated.
//Calculate hours and minutes from the variable holding the duration
var hours = Math.floor(duration_minutes/60)
var minutes = duration_minutes%60
//Hours less than 10 need a prefix of 0
if (hours < 10){
hours = 0 + hours.toString();
}
else{
hours = hours.toString();
}
//Minutes less than 10 need a prefix of 0
if (minutes < 10){
minutes = 0 + minutes.toString();
}
else{
minutes = minutes.toString();
}
//Join hours and minutes together to make HH:MM format
var fullduration = hours + ":" + minutes;
If duration_minutes is 150, fullduration will become 02:30.

Related

How do I create a DateTime instance in a specific timezone?

I am calling a legacy service that returns all times in a string format assuming that the timezone is EST. How do I create a DateTime instance and specify that the timezone is EST? When I display the times to the user's I will translate to their device timezone.
I wrote a package for this. It's called Instant, and it can fetch the latest DateTime in any given zone worldwide. Take a detailed look at https://aditya-kishore.gitbook.io/instant/
The basic usage to get a DateTime in a timezone is fairly simple.
Note: I assume below that your legacy service delivers a String with format HH:MM in 24 hr time, and that it doesn't deliver a date. If any of the following is untrue, lemme know and I'll change the code sample.
List<int> _hourmin (String parse) {
List<int> timeinint = [];
if (parse.length == 5) {
//if parse is something like 10:00
timeinint.add(int.parse(parse.substring(0,2))); //Add hours (10)
timeinint.add(int.parse(parse.substring(3,5))); //Add minutes (0)
} //hours >= 10
if (parse.length == 4) {
//if parse is something like 4:06
timeinint.add(int.parse(parse.substring(0,1))); //Add hours (4)
timeinint.add(int.parse(parse.substring(2,4))); //Add minutes (6)
} //hours < 10
return timeinint;
}
DateTime utcDateTimeFromZone(
{#required String zone,
int year = 0,
int month = 0,
int day = 0,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0}) {
hour = (hour - timeZoneOffsets[zone].truncate()) % 24; //Convert to UTC from timezone
if (hour<0) {
hour = 24 + hour;
}
minute = (minute - ((timeZoneOffsets[zone]%1)*60).round()) % 60;
if (minute<0) {
minute = 60 + minute;
}
//Convert to UTC from timezone
DateTime inUTC = DateTime.utc(year, month, day, hour, minute, second, millisecond, microsecond); //Initialize in UTC time
return inUTC;
}
//We get an array containing the hours minutes
List<int> time = _hourmin("4:25"); //put your API call as the param here
//This is what you need
DateTime finished = utcDateTimeFromZone(zone: "EST", hour: time[0], minute: time[1]);
print(formatTime(time: finished.toLocal())); //1:25 for me, because I'm PST, but this works with any local timezone
Hope that clears it up!
The included DateTime class only supports the local timezone or UTC but no specific timezone.
https://pub.dartlang.org/packages/timezone would provide that but it looks like it needs an update for Dart 2.0.0
https://pub.dartlang.org/packages/time_machine is a newer one.

Is there a way to detect which millennium a NSDate is in?

I have an NSDate from a string of 01/01/14. Unfortunately the strings are saved with two digits so I have no idea if the year is from 1900's or 2000's. But I want create some intelligent code to guess what millennium and century the full year is. I know for a fact (based on the application I'm creating) that the year can't be below 1900 in 99% of cases.
I have converted the date to now show four digits on but it's now being displayed as 01/01/0014.
Is there a way to detect the millennium of the NSDate as 0 and make my changes accordingly?
What I really want to do is something like this (pseudo code):
if (millennium == 0 && decade < 15 && decade > 99)
{
millennium = 2;
century = 0;
// change the date to 2000's here
}
else if (millennium == 00)
{
millennium = 1;
century = 9;
// change the date to 1900's here
}
For starters your pseudocode isn't quite right (the first if statement is an impossible condition as decade can't be less than 15 and more than 99 at the same time).
I have created a basic solution using NSDateComponents and made up my own conditions for when to change the year which you can easily change.
NSDate *date; // this is your date
NSDateComponents* components = [[NSCalendar currentCalendar] components: NSMonthCalendarUnit | NSDateCalendarUnit | NSYearCalendarUnit fromDate:date];
if([components year] < 15) // 2000 to 2015
{
[components setYear:[components year] + 2000];
}
else if([components year] < 100) // 1915 to 1999
{
[components setYear:[components year] + 1900];
}
date = [NSCalendar currentCalendar] dateFromComponents:components];
there is an oldschool stuff (from the 80s) here to convert a year in 2 digits to year in 4 digits.
int finalYear = (19 + (yearIn2Digits < 50)) * 100 + yearIn2Digits;
then you can use that finalYear value to build up the proper NSDate object.
the covered interval is always 100 years and it is up you where you'd like to draw the line. the original idea worked between 1950 and 2049 – as you see in the formula, – but you can refine it for covering better your actual bounds of years.
I wouldn't recommend hardcoding this logic.
what if you want to convert a date which is 1908 , or 2015.
A smarter way to do this is as follows :
-(BOOL)isInCurrentDecade:(NSDate*)date{
// I don't recollect the api to extract year from NSDate
NSInteger tmpYear = [date year];
NSInteger currentYear = [[NSDate date] year];
return tmpYear > currentYear%100
}
What I am doing here is that I compare 87 , 04 , 99 with current year digit's 14. and if it's between 00 and now then its current decade. This code is more robust than your's because it's relative comparison with current date
o/p of the code is as follow as of year 2014 :
87 - > NO // in 90's
99 -> NO
04 - > YES // in 2000's
12 - > YES
Edge case occurs when your date includes 1/1/14 if you want it to be filtered in current decade replace the '>' with ' >=" .
Use NSDateFormatter and its property "twoDigitStartDate". You set this to the earliest date that you want to be able to enter with a two digit year. No complicated logic needed, and easy to update. Now data entry is one thing, but storing a date "with a two digit year", that's criminal.
And it handles things like "I want to be able to store anything starting with the start of the tax year 1985 in two digits, that is April 1st 1985". Then April 1st 85 will be in 1985, while March 31st 85 will be in 2085. With no effort.

Compare time (in format HH:MM:SS) if between two times in IOS

I have the start time and end time in formats HH:mm:ss (24-hour).
I need to compare if the current time is between the start and end time.
I have coded the logic in such a way that it will add 24 hrs to the start time if start time is later than the end time.
if (shift.sShiftStart < shift.sShiftEnd) {
startTime = shift.sShiftStart;
startTimeInt = [[shift.sShiftStart substringToIndex:2] integerValue];
startTimeInt2 = startTimeInt + 24;
finalStartTime = [startTime stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:[NSString stringWithFormat:#"%d", startTimeInt2]];
} else {
finalStartTime = shift.sShiftStart;
}
Now I want to check if my currentTime is between finalStartTime and endTime (all are in HH:mm:ss formats)
Convert the strings to dates (NSDate) and use the compare: method. Don't assume that you can use < on object instances and they will magically know what you want them to do with that (you're actually comparing the pointer values).
Once you have the dates you can add time (also look at the NSDateComponents class).

hh:mm aa (12 hour format ) convert in HH:mm (24 hour format)

I have String time="02:30 PM" means 12 hour format and i want to convert this time in 24 hour format .
I want this o/p: 14:30. so how can i convert this ?
I don't know anything about berries, but in case the API is missing a proper formatting function, you can always get your hands dirty with the string itself:
static String convert(String time){
boolean pm = "PM".equals(time.substring(6).toUpperCase());
int h = Integer.valueOf(time.substring(0,2));
if (h!=12)
h+=pm?12:0;
else
h=pm?h:0;
return ((h<10)?"0":"")+h + ":" + time.substring(3,5);
}
Try this code.
This will give the current time in 24 Hours format.
SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
String newTime = formate.formatLocal(System.currentTimeMillis());

Convert date into julian date

I want to convert todays date into the julian date format . Suppose if the date is 31/12/2011(31st dec 2011) , then the julian date should be 11365(yyddd) .
Any help would be much appreciated
Thanks
One simple way to get the date in yyddd format would be doing some mathematics:
date theDate = 31\12\2011;
int theOrdDate;
;
theOrdDate = 1000 * (year(theDate) mod 100) + dayOfYr(theDate);
// or
theOrdDate = 1000 * (year(theDate) - 2000) + dayOfYr(theDate);
EDIT: one possible drawback: this calculation will return only 4 digits if the year is less than 2010.
In AX2012 this task is super easy.
str myJulian;
myJulian = date2Julian(today());

Resources