Created
February 17, 2022 16:13
-
-
Save shubham836/131edb5b89f379dfaa575431b756f0eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main(args: Array<String>) { | |
| print(getDifferenceInHours("1:00 PM", "2:00 AM", "15", "16")) | |
| } | |
| //14 10 | |
| fun getDifferenceInHours(startTime: String, endTime: String, startDate: String, endDate: String): Int { | |
| if (endDate == startDate) { | |
| return inFullDayFormat(endTime).toInt() - inFullDayFormat(startTime).toInt() | |
| } else { | |
| val startTimeWithSeconds= inFullDayFormat(startTime) | |
| val endTimeWithSeconds= inFullDayFormat(endTime) | |
| var first =(24 - startTimeWithSeconds.split(":").first().toInt()).toString()+":"+startTimeWithSeconds.split(":").last().toString() | |
| if (endTimeWithSeconds.split(":").last().toInt() > startTimeWithSeconds.split(":").last().toInt() ){ | |
| first =(first.split(":").first().toInt()+1).toString().split(":").first() | |
| }else{ | |
| first= first.split(":").first() | |
| } | |
| val second =inFullDayFormat(endTime).split(":").first().toInt() | |
| return first.toInt() + second | |
| } | |
| } | |
| fun inFullDayFormat(time: String): String { | |
| if (time.contains("AM")) { | |
| return time.split(" ").first() | |
| } else { | |
| val temp = (12 + time.split(" ").first().toString().split(":").first().toInt()).toString() | |
| return temp+":"+time.split(" ").first().split(":").last() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment