Skip to content

Instantly share code, notes, and snippets.

@areagray
Created January 14, 2014 15:27
Show Gist options
  • Save areagray/8420073 to your computer and use it in GitHub Desktop.
Save areagray/8420073 to your computer and use it in GitHub Desktop.
Coderbyte: Using the JavaScript language, have the function CountingMinutesI(str) take the str parameter being passed which will be two times (each properly formatted with a colon and am or pm) separated by a hyphen and return the total number of minutes between the two times. The time will be in a 12 hour clock format. For example: if str is 9:…
//Coderbyte Challenge
function CountingMinutesI(str) {
var when,
offset,
mins,
segments = [],
ampm,
hours,
totalMins=[],
diff;
//parse parameters
str=str.split('-');
for (var i = 0; i<str.length; i++) {
//grab am pm suffix
ampm = str[i].slice(str[i].length-2);
str[i]= str[i].substr(0, str[i].length-2);
//split the rest
segments = str[i].split(':');
hours= parseInt(segments[0]);
mins=parseInt(segments[1]);
//normalize mins
if ((hours==12) && (ampm=='am')){
console.log('zeroing hours');
hours=0;
}
if ((ampm=='pm') && (hours != 12)){
hours = hours+12;
}
combomins=((hours*60)+mins);
totalMins.push((hours * 60) + mins);
}
//account for next day
if (totalMins[1]<totalMins[0]){
totalMins[1] += 1440;
}
return totalMins[1]-totalMins[0];
}
@smyy96
Copy link

smyy96 commented Nov 19, 2023

using System;

class MainClass {

public static string StringChallenge(string str) {

string ampm;
string[] segment;
int[] totalmin= new int[2];
int hour=0,mins=0, combomins=0;
string[] time = str.Split("-"); 

for(var i=0;i<time.Length;i++)
{
  ampm=time[i].Substring(time[i].Length-2);
  time[i]=time[i].Substring(0,time[i].Length-2);
  segment=time[i].Split(":");
  hour=int.Parse(segment[0]);
  mins=int.Parse(segment[1]);

  if((hour==12)&&(ampm=="am"))
    hour=0;
  if((ampm=="pm")&&(hour!=12))
    hour+=12;

  combomins=((hour*60)+mins);
  totalmin[i]=combomins;

}

if(totalmin[1]<totalmin[0])
  totalmin[1]+=1440;

string value=(totalmin[1]-totalmin[0]).ToString();
return value;

}

static void Main() {

// keep this function call here
Console.WriteLine(StringChallenge(Console.ReadLine()));

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment