Given two strings str1 and str2 which represent two dates, the task is to count the number of days between given two dates. Given that dates given are beyond 1971. Examples:
Input: str1 = “2020-01-29”, str2 = “2020-01-30” Output: 1 Explanation: The number of days between 29th January and 30th January is 1. Input: str1 = “1971-06-29”, str2 = “2019-06-23” Output: 17526
Approach: The idea is to convert both the dates into its respective seconds. Since there are 86, 400 seconds in a day, the number of days between both the days can be found out by dividing the difference of seconds with 86, 400. Below is the implementation of the above approach:
CPP
// C++ implementation of the above approach #include <iostream> #include <sstream> #include <string> using namespace std; // Function to find the number of days // between given two dates int daysBetweenDates(string date1, string date2) { stringstream ss(date1 + "-" + date2); int year, month, day; char hyphen; // Parse the first date into seconds ss >> year >> hyphen >> month >> hyphen >> day; struct tm starttm = { 0, 0, 0, day, month - 1, year - 1900 }; time_t start = mktime (&starttm); // Parse the second date into seconds ss >> hyphen >> year >> hyphen >> month >> hyphen >> day; struct tm endtm = { 0, 0, 0, day, month - 1, year - 1900 }; time_t end = mktime (&endtm); // Find out the difference and divide it // by 86400 to get the number of days return abs (end - start) / 86400; } // Driver code int main() { string str1 = "2019-09-12" ; string str2 = "2020-08-04" ; cout << daysBetweenDates(str1, str2); return 0; } |
Java
// Java implementation of the above approach import java.util.*; import java.time.*; import java.time.format.*; import java.time.temporal.*; class GFG { // Function to find the number of days // between given two dates static long daysBetweenDates(String date1, String date2) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern( "yyyy MM dd" ); String[] ss = (String.valueOf(date1) + "-" + String.valueOf(date2)) .split( "-" ); String year, month, day; year = ss[ 0 ]; month = ss[ 1 ]; day = ss[ 2 ]; LocalDate start = LocalDate.parse(year + " " + month + " " + day , dtf); year = ss[ 3 ]; month = ss[ 4 ]; day = ss[ 5 ]; LocalDate end =LocalDate.parse(year + " " + month + " " + day, dtf); return ChronoUnit.DAYS.between(start, end); } // Driver code public static void main(String[] args) { String str1 = "2019-09-12" ; String str2 = "2020-08-04" ; System.out.println(daysBetweenDates(str1, str2)); } } // This code is contributed by phasing17 |
Python3
# Python3 implementation of the above approach import time, datetime, math # Function to find the number of days # between given two dates def daysBetweenDates( date1, date2): ss = [ int (i) for i in (date1 + "-" + date2).split( "-" )] # Parse the first date into seconds year = ss[ 0 ] month = ss[ 1 ] day = ss[ 2 ]; start = datetime.datetime(year, month, day); # Parse the second date into seconds year = ss[ 3 ] month = ss[ 4 ] day = ss[ 5 ]; end = datetime.datetime(year, month, day); end = time.mktime(end.timetuple()) start = time.mktime(start.timetuple()) diff = (start - end) # Find out the difference and divide it # by 86400 to get the number of days return math.ceil( abs (diff) / 86400 ) # Driver code str1 = "2019-09-12" ; str2 = "2020-08-04" ; print (daysBetweenDates(str1, str2)); # This code is contributed by phasing17 |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function to find the number of days // between given two dates static int daysBetweenDates( string date1, string date2) { string [] ss = (Convert.ToString(date1) + "-" + Convert.ToString(date2)) .Split( '-' ); int [] ss1 = new int [ss.Length]; for ( int i = 0; i < ss.Length; i++) ss1[i] = Convert.ToInt32(ss[i]); // Parse the first date into seconds var start = new DateTime(ss1[0], ss1[1], ss1[2]); // Parse the second date into seconds var end = new DateTime(ss1[3], ss1[4], ss1[5]); // Find out the difference and divide it // by 86400 to get the number of days return (end - start).Days; } // Driver code public static void Main( string [] args) { string str1 = "2019-09-12" ; string str2 = "2020-08-04" ; Console.WriteLine(daysBetweenDates(str1, str2)); } } // This code is contributed by phasing17 |
Javascript
// JS implementation of the above approach // Function to find the number of days // between given two dates function daysBetweenDates( date1, date2) { let ss = (date1 + "-" + date2).split( "-" ).map( function (a) { return parseInt(a)}); let year, month, day; let hyphen; // Parse the first date into seconds year = ss[0] month = ss[1] day = ss[2]; let start = new Date(year, month, day); // Parse the second date into seconds year = ss[3] month = ss[4] day = ss[5]; let end = new Date(year, month, day - 1); // Find out the difference and divide it // by (1000 * 60 * 60 * 24) to get the number of days return Math.floor(Math.abs(end - start) / (1000 * 60 * 60 * 24) ); } // Driver code let str1 = "2019-09-12" ; let str2 = "2020-08-04" ; console.log(daysBetweenDates(str1, str2)); // This code is contributed by phasing17 |
327
Time Complexity: O(1)
Auxiliary Space: O(1), As constant extra space is used.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!