OC获取本周日期
来源:互联网 发布:软件开发和互联网 编辑:程序博客网 时间:2024/06/05 07:18
NSDate *nowDate = [NSDatedate];
NSCalendar *calendar = [NSCalendarcurrentCalendar];
NSDateComponents *comp = [calendarcomponents:NSYearCalendarUnit |NSMonthCalendarUnit | NSDayCalendarUnit |NSWeekdayCalendarUnit | NSDayCalendarUnitfromDate:nowDate];
//获取今天是周几
NSInteger weekDay = [compweekday];
//获取几天是几号
NSInteger day = [compday];
//计算当前日期和本周的星期一和星期天相差天数
long firstDiff,lastDiff;
// weekDay = 1;
if (weekDay ==1)
{
firstDiff = -6;
lastDiff =0;
}
else
{
firstDiff = [calendarfirstWeekday] - weekDay + 1;
lastDiff =8 - weekDay;
}
// NSLog(@"firstDiff: %ld lastDiff: %ld",firstDiff,lastDiff);
//在当前日期(去掉时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = [calendarcomponents:NSYearCalendarUnit |NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
[firstDayCompsetDay:day + firstDiff];
NSDate *firstDayOfWeek = [calendardateFromComponents:firstDayComp];
NSDateComponents *lastDayComp = [calendarcomponents:NSYearCalendarUnit |NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
[lastDayComp setDay:day + lastDiff];
NSDate *lastDayOfWeek = [calendardateFromComponents:lastDayComp];
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"dd"];
NSString *firstDay = [formatterstringFromDate:firstDayOfWeek];
NSString *lastDay = [formatterstringFromDate:lastDayOfWeek];
// NSLog(@"%@=======%@",firstDay,lastDay);
int firstValue = firstDay.intValue;
int lastValue = lastDay.intValue;
NSMutableArray *dateArr = [[NSMutableArrayalloc]init];
if (firstValue < lastValue) {
for (int j =0; j<7; j++) {
NSString *obj = [NSStringstringWithFormat:@"%d",firstValue+j];
[dateArraddObject:obj];
}
}
elseif (firstValue > lastValue)
{
for (int j =0; j < 7-lastValue; j++) {
NSString *obj = [NSStringstringWithFormat:@"%d",firstValue+j];
[dateArraddObject:obj];
}
for (int z =0; z<lastValue; z++) {
NSString *obj = [NSStringstringWithFormat:@"%d",z+1];
[dateArraddObject:obj];
}
}
- OC获取本周日期
- 根据当前日期获取本周日期
- php获取本周日期,及本周起始时间
- OC获取日期时间
- 根据当前日期获取本周起止时间
- 根据当前日期获取本周起止时间
- JAVA-获取本周的起始日期
- java获取本周第一天的日期
- java 获取本周一的日期
- php根据给定日期获取给定日期本周的日期
- java获取当前日期时间和本周日期时间
- 根据当前日期 获取 本周 ,本月 的起止日期
- 获取本周、本季度、本月、上月的开端日期、停止日期
- php获取本周日期,自定义日期 开始结束时间
- php获取本周的开始日期和结束日期
- java 根据当前日期获取本周或上周日期区间
- java 根据当前日期获取本周或上周日期区间
- 用php获取本周,上周,本月,上月,本季度日期
- ArrayIndexOutOfBoundsException listview 不同视图问题
- Linux系统网络性能实例分析
- Windows MySQL BAT脚本控制启动、停止、重启
- MySQL-临时表
- luajit 使用云风的int64库 负数 bad light userdata pointer
- OC获取本周日期
- ORA-28390: auto login wallet not open but encryption wallet may be open
- myeclipse6.5 debug 无法进入jsp页面
- MySQL 加锁处理分析
- 第九周项目二 对称矩阵压缩存储的实现与应用(1)
- JAVA中的内部类
- Linux创建用户只能访问某个目录
- AJAX兼容IE6的方法
- 第9周项目4-广义表算法库及应用(1)