Salesforce OA 面试真题解析:手动计算两个日期之间的天数

24次阅读
没有评论

Given 2 dates in the format yyyy/mm/dd, return the number of days between them.

Do not use any library; parse it manually. Consider leap years and different month lengths.

这道题要求手动解析两个 <code>yyyy/mm/dd</code> 格式的日期,并计算它们相差的天数,不能依赖现成日期库。核心做法通常是把日期转换成“从某个起点开始的累计天数”,再做差值;其中需要正确处理每个月的天数以及闰年规则(2 月天数在闰年时为 29)。实现时可以先编写判断闰年的函数,再按年份、月份、日期逐步累加,最后得到两个日期之间的天数差。

正文完
 0