iOS 获取本周,本月,本季,的起止日期

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; background-color: rgba(160, 223, 163, 0.15) }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81; background-color: rgba(160, 223, 163, 0.15) }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400; background-color: rgba(160, 223, 163, 0.15) }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; background-color: rgba(160, 223, 163, 0.15); min-height: 14.0px }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; background-color: rgba(160, 223, 163, 0.15); min-height: 21.0px }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b; background-color: rgba(160, 223, 163, 0.15) }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa; background-color: rgba(160, 223, 163, 0.15) }
span.s1 { color: #ba2da2 }
span.s2 { color: #703daa }
span.s3 { color: #3e1e81 }
span.s4 { color: #000000 }
span.s5 { color: #d12f1b }
span.s6 { color: #31595d }
span.s7 { color: #272ad8 }

- (void)getQuarterBeginAndEndWith:(NSDate *)currentDate{

NSDate *nowDate = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:nowDate];

/**获取当前的月份*/

NSInteger curentMonth = [comp month];

NSString * currentYear = [NSString stringWithFormat:@"%ld",[comp year]];

NSString * beginTime = [self getQuarterStartDateWithYear:currentYear currentMonth:curentMonth];

NSString * endTime = [self getQuarterEndDateWithYear:currentYear currentMonth:curentMonth];

NSString * str = [NSString stringWithFormat:@"季度开始时间 == %@  季度结束时间 == %@",beginTime,endTime];

NSLog(@"%@",str);

}

//获得某季度的开始日期

- (NSString *)getQuarterStartDateWithYear:(NSString *)paraYear currentMonth:(NSInteger)currentMonth {

NSString * beginTime = @"";

if (currentMonth < 4) {

beginTime = [paraYear stringByAppendingString:@"-01-01"];

} else if (currentMonth >= 4 && currentMonth < 7) {

beginTime = [paraYear stringByAppendingString:@"-04-01"];

} else if (currentMonth >= 7 && currentMonth < 10) {

beginTime = [paraYear stringByAppendingString:@"-07-01"];

} else if (currentMonth >= 10 && currentMonth < 12) {

beginTime = [paraYear stringByAppendingString:@"-10-01"];

}

return beginTime;

}

//获得某季度的结束日期

- (NSString *)getQuarterEndDateWithYear:(NSString *)paraYear currentMonth:(NSInteger)currentMonth {

NSString * endTime = @"";

if (currentMonth < 4) {

endTime = [paraYear stringByAppendingString:@"-03-31"];

} else if (currentMonth >= 4 && currentMonth < 7) {

endTime = [paraYear stringByAppendingString:@"-06-30"];

} else if (currentMonth >= 7 && currentMonth < 10) {

endTime = [paraYear stringByAppendingString:@"-09-30"];

} else if (currentMonth >= 10 && currentMonth < 12) {

endTime = [paraYear stringByAppendingString:@"-12-31"];

}

return endTime;

}

- (void)getWeekBeginAndEndWith:(NSDate *)newDate{

NSDate *nowDate = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:nowDate];

// 获取今天是周几

NSInteger weekDay = [comp weekday];

/**获取当前几个月*/

NSInteger monthDay = [comp month];

NSLog(@"%ld",monthDay);

// 获取几天是几号

NSInteger day = [comp day];

NSLog(@"%ld----%ld",weekDay,day);

// 计算当前日期和本周的星期一和星期天相差天数

long firstDiff,lastDiff;

//    weekDay = 1;

if (weekDay == 1)

{

firstDiff = -6;

lastDiff = 0;

}

else

{

firstDiff = [calendar firstWeekday] - weekDay + 1;

lastDiff = 8 - weekDay;

}

NSLog(@"firstDiff: %ld   lastDiff: %ld",firstDiff,lastDiff);

// 在当前日期(去掉时分秒)基础上加上差的天数

NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];

[firstDayComp setDay:day + firstDiff];

NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];

NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay   fromDate:nowDate];

[lastDayComp setDay:day + lastDiff];

NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"MM月dd日"];

NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];

NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];

NSLog(@"%@=======%@",firstDay,lastDay);

NSString *dateStr = [NSString stringWithFormat:@"%@-%@",firstDay,lastDay];

NSLog(@"%@",dateStr);

}

- (void)getMonthBeginAndEndWith:(NSDate *)newDate{

if (newDate == nil) {

newDate = [NSDate date];

}

double interval = 0;

NSDate *beginDate = nil;

NSDate *endDate = nil;

NSCalendar *calendar = [NSCalendar currentCalendar];

[calendar setFirstWeekday:2];

//设定周一为周首日

BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate]; //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit

if (ok) {

endDate = [beginDate dateByAddingTimeInterval:interval-1];

}else {

return;

}

NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];

[myDateFormatter setDateFormat:@"yyyy.MM.dd"];

NSString *beginString = [myDateFormatter stringFromDate:beginDate];

NSString *endString = [myDateFormatter stringFromDate:endDate];

NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];

NSLog(@"%@",s);

}

原文地址:https://www.cnblogs.com/Jackie-subDai/p/9552540.html

时间: 2024-10-08 23:42:07

iOS 获取本周,本月,本季,的起止日期的相关文章

PHP 时间获取本周 本月 本季度用法

<?php        $week_begin = mktime(0, 0, 0,date("m"),date("d")-date("w")+1,date("Y"));$week_end = mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")); echo $week_begi

Oracle 获取本周、本月、本季、本年的第一天和最后一天

Oracle 获取本周.本月.本季.本年的第一天和最后一天 --本周 select trunc(sysdate, 'd') + 1 from dual; select trunc(sysdate, 'd') + 7 from dual; --本月 select trunc(sysdate, 'mm') from dual; select last_day(trunc(sysdate)) from dual; --本季 select trunc(sysdate, 'Q') from dual; s

SQL Server 获取本周,本月,本年等时间内记录

datediff(week,zy_time,getdate())=0 //查询本周 datediff(month,zy_time,getdate())=0 //查询本月 本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0 前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7 1. 当前系

* 获取本周、本季度、本月、上月的开始日期、结束日期

/** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date();                    //当前日期 var nowDayOfWeek = now.getDay();         //今天本周的第几天 var nowDay = now.getDate();              //当前日 var nowMonth = now.getMonth();           //当前月 var nowYear = now.getY

JS获取本周、本季度、本月、上月的开始日期、结束日期

/** * 获取本周.本季度.本月.上月的开始日期.结束日期 */var now = new Date(); //当前日期 var nowDayOfWeek = now.getDay(); //今天本周的第几天 var nowDay = now.getDate(); //当前日 var nowMonth = now.getMonth(); //当前月 var nowYear = now.getYear(); //当前年 nowYear += (nowYear < 2000) ? 1900 : 0

c# 获取 本周、本月、本季度、本年 的开始时间或结束时间

#region 获取 本周.本月.本季度.本年 的开始时间或结束时间 /// <summary> /// 获取结束时间 /// </summary> /// <param name="TimeType">Week.Month.Season.Year</param> /// <param name="now"></param> /// <returns></returns>

JS获取本周、本季度、本月、上月的开端日期、停止日期

Js代码 /** * 获取本周.本季度.本月.上月的开端日期.停止日期 */ var now = new Date(); //当前日期 var nowDayOfWeek = now.getDay(); //今天本周的第几天 var nowDay = now.getDate(); //当前日 var nowMonth = now.getMonth(); //当前月 var nowYear = now.getYear(); //当前年 nowYear += (nowYear < 2000) ? 19

C#获取本周、上周、本月、上月、本季度、上季度、本年、上一年起始时间和结束时间

[参考] http://blog.csdn.net/livening/article/details/6049341 http://zhidao.baidu.com/question/378600365.html http://www.cnblogs.com/roy117/archive/2008/03/25/1121584.html 楼上几层的代码都太多了,不用那么复杂. 先跟你说一下原理: 1.国际上的WeekDay是从周日到周六的顺序 2.再说C#的DayOfWeek枚举值是依次从0到6,

oracle sql语句取得本周本月本年的数据

[sql] --国内从周一到周日 国外是周日到周六  select to_char(sysdate-1,'D') from dual;--取国内的星期几 去掉减一取国外的星期-- [sql] --取本周时间内的数据  select * from table  where DTIME >=trunc(next_day(sysdate-8,1)+1) and DTIME<=trunc(next_day(sysdate-8,1)+7)+1 ;     select * from table  whe