题目描述
请你编写一个程序来计算两个日期之间隔了多少天。日期以字符串形式给出,格式为 YYYY-MM-DD,如示例所示。示例 1:输入:date1 = "2019-06-29", date2 = "2019-06-30"
输出:1
示例 2:输入:date1 = "2020-01-15", date2 = "2019-12-31"
输出:15来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-days-between-two-dates
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一眼定真,windowsAPI,我就是调包侠!
public int DaysBetweenDates(string date1, string date2){var arr1 = date1.Split('-');var arr2 = date2.Split('-');DateTime dateTime1 = new DateTime(Convert.ToInt32(arr1[0]), Convert.ToInt32(arr1[1]), Convert.ToInt32(arr1[2]));DateTime dateTime2 = new DateTime(Convert.ToInt32(arr2[0]), Convert.ToInt32(arr2[1]), Convert.ToInt32(arr2[2]));return Math.Abs((int)(dateTime2 - dateTime1).TotalDays);}