项目需求用到,以下代码有部分是根据iOS修改而来。
直接上代码了。
1. 获取当前时间
// MARK: 获取当前时间
// 这里的 type 是指日期的格式 “yyyyMMdd” 或者 “yyyy-MM-dd” 这样子
class func nowTime(_ type: String?) -> String {
let currentDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = type ?? "yyyyMMdd"
let time = formatter.string(from: currentDate)
return time
}
2. 获取当前时间的前一天(后一天)
这个方法可以用来获取相对与今天的任意一天时间
// MARK: 前一天的时间
// nowDay 是传入的需要计算的日期
class func getLastDay(_ nowDay: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
// 先把传入的时间转为 date
let date = dateFormatter.date(from: nowDay)
let lastTime: TimeInterval = -(24*60*60) // 往前减去一天的秒数,昨天
// let nextTime: TimeInterval = 24*60*60 // 这是后一天的时间,明天
let lastDate = date?.addingTimeInterval(lastTime)
let lastDay = dateFormatter.string(from: lastDate!)
return lastDay
}
以上代码对应的iOS代码是:
// 得到以前的某个日期
- (NSString *)getAgoTime:(NSString *)second
{
//得到当前的时间
NSDate * date = [NSDate date];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
//设置时间间隔(秒)(这个我是计算出来的,不知道有没有简便的方法 )
NSTimeInterval time = [second intValue];//一年的秒数
//得到一年之前的当前时间(-:表示向前的时间间隔(即去年),如果没有,则表示向后的时间间隔(即明年))
NSDate * lastYear = [date dateByAddingTimeInterval:-time];
//转化为字符串
NSString * startDate = [dateFormatter stringFromDate:lastYear];
return startDate;
}
3. 获取某一天所在周一和周日的日期
// MARK: 获取某一天所在的周一和周日
class func getWeekTime(_ dateStr: String) -> Array<String> {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let nowDate = dateFormatter.date(from: dateStr)
let calendar = Calendar.current
let comp = calendar.dateComponents([.year, .month, .day, .weekday], from: nowDate!)
// 获取今天是周几
let weekDay = comp.weekday
// 获取几天是几号
let day = comp.day
// 计算当前日期和本周的星期一和星期天相差天数
var firstDiff: Int
var lastDiff: Int
// weekDay = 1;
if (weekDay == 1) {
firstDiff = -6;
lastDiff = 0;
} else {
firstDiff = calendar.firstWeekday - weekDay! + 1
lastDiff = 8 - weekDay!
}
// 在当前日期(去掉时分秒)基础上加上差的天数
var firstDayComp = calendar.dateComponents([.year, .month, .day], from: nowDate!)
firstDayComp.day = day! + firstDiff
let firstDayOfWeek = calendar.date(from: firstDayComp)
var lastDayComp = calendar.dateComponents([.year, .month, .day], from: nowDate!)
lastDayComp.day = day! + lastDiff
let lastDayOfWeek = calendar.date(from: lastDayComp)
let firstDay = dateFormatter.string(from: firstDayOfWeek!)
let lastDay = dateFormatter.string(from: lastDayOfWeek!)
let weekArr = [firstDay, lastDay]
return weekArr;
}
以上代码对应的iOS代码是:
#pragma mark - 获取某一天所在的周一和周日
+ (NSString *)getWeekTime:(NSString *)dateStr
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *nowDate = [formatter dateFromString:dateStr];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = nil;
if (DEV_VER >= 8.0) {
comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
} else {
comp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
}
// 获取今天是周几
NSInteger weekDay = [comp weekday];
// 获取几天是几号
NSInteger day = [comp day];
// 计算当前日期和本周的星期一和星期天相差天数
long firstDiff,lastDiff;
// weekDay = 1;
if (weekDay == 1) {
firstDiff = -6;
lastDiff = 0;
} else {
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 8 - weekDay;
}
// 在当前日期(去掉时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = nil;
NSDateComponents *lastDayComp = nil;
if (DEV_VER >= 8.0) {
firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
} else {
firstDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
}
[firstDayComp setDay:day + firstDiff];
NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
if (DEV_VER >= 8.0) {
lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
} else {
lastDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
}
[lastDayComp setDay:day + lastDiff];
NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
NSString *weekStr = [NSString stringWithFormat:@"%@ 至 %@", firstDay, lastDay];
return weekStr;
}
4. 获取某一日所在月份的开始和结束日期
// MARK: 当月开始日期
// nowDay 为传入需要计算的日期
class func startOfCurrentMonth(_ nowDay: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let nowDayDate = dateFormatter.date(from: nowDay)
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month], from: nowDayDate!)
let startOfMonth = calendar.date(from: components)
let day = dateFormatter.string(from: startOfMonth!)
return day
}
// MARK: 当月结束日期
class func endOfCurrentMonth(_ nowDay: String, returnEndTime:Bool = false) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let nowDayDate = dateFormatter.date(from: nowDay)
let calendar = Calendar.current
var components = DateComponents()
components.month = 1
if returnEndTime {
components.second = -1
} else {
components.day = -1
}
//startOfCurrentMonth
let currentMonth = calendar.dateComponents([.year, .month], from: nowDayDate!)
let startOfMonth = calendar.date(from: currentMonth)
let endOfMonth = calendar.date(byAdding: components, to: startOfMonth!)
let day = dateFormatter.string(from: endOfMonth!)
return day
}
5. 获取上一周的日期和获取上一个月的日期
当然是先根据上述方法获取本周(本月)的第一天,然后获取前一天,也就是上周(上月)的最后一天,然后再去重新获取那一周(那个月)的日期了。
以上所使用的格式都是 yyyyMMdd
的格式,在使用的过程中大家可以自己设置成需要的格式。 另外以上所有的 nowDay
都是你传入的需要计算的那一天的日期格式,是 String
类型的。