即:
NSDate *firstDayOfWeek = [[NSDate date] firstDayOfWeek];
例如,今天是8月19日,我想从上面的代码行中获得NSDate
2010-08-15 12:00am
。谢谢!
答案 0 :(得分:36)
我认为这些主题会回应你所寻找的内容:http://www.cocoabuilder.com/archive/cocoa/211648-nsdatecomponents-question.html#211826
但请注意,它不会将星期一作为一周的第一天处理,因此您可能需要通过减去[gregorian firstWeekday]
而不仅仅1
来稍微调整一下。另外,我修改它以使用-currentCalendar
,但这取决于你: - )
NSDate *today = [NSDate date];
NSCalendar *gregorian = [NSCalendar currentCalendar];
// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
/*
Create a date components to represent the number of days to subtract
from the current date.
The weekday value for Sunday in the Gregorian calendar is 1, so
subtract 1 from the number
of days to subtract from the date in question. (If today's Sunday,
subtract 0 days.)
*/
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
/* Substract [gregorian firstWeekday] to handle first day of the week being something else than Sunday */
[componentsToSubtract setDay: - ([weekdayComponents weekday] - [gregorian firstWeekday])];
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];
/*
Optional step:
beginningOfWeek now has the same hour, minute, and second as the
original date (today).
To normalize to midnight, extract the year, month, and day components
and create a new date from those components.
*/
NSDateComponents *components = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate: beginningOfWeek];
beginningOfWeek = [gregorian dateFromComponents: components];
答案 1 :(得分:21)
为什么这么复杂? :)
func firstDateOfWeekWithDate(date: NSDate) -> NSDate {
var beginningOfWeek: NSDate?
calendar.rangeOfUnit(.WeekOfYear, startDate: &beginningOfWeek, interval: nil, forDate: date)
return beginningOfWeek!
}
答案 2 :(得分:11)
如果星期一(2)设置为第一周,并且您正在检查的日期是星期日,则naixn的解决方案将产生错误的结果。在这种情况下,你将得到下周一的星期一。
正确的方法是按如下方式计算减法分量:
[componentsToSubtract setDay: - ((([weekdayComponents weekday] - [gregorian firstWeekday])
+ 7 ) % 7)];
假设你一周有7天。
答案 3 :(得分:2)
- (NSDate *)firstDateOfWeekWithDate:(NSDate *)date {
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents *weekdayComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
[components setDay:-((([weekdayComponents weekday] - [calendar firstWeekday]) + 5 ) % 7)];
return [calendar dateFromComponents:components];
}
答案 4 :(得分:2)
这已经得到了解答,但是Wolfgang has pointed out,接受的答案中有一个错误。如果将第一个工作日设置为比相关UPDATE `myTable` SET `CastLotNo` = (SELECT `LotNo` FROM `MyTable` WHERE `OperationCode` = '1280' LIMIT 1) WHERE `CastTID` = (SELECT `CastTID` FROM `MyTable` WHERE `OperationCode` = '1280' LIMIT 1)
更大的日期,则结果将不正确。这是一个适用于所有可能的NSDate
值的完整解决方案(该示例使用星期一)。
firstWeekday
答案 5 :(得分:1)
最简单的解决方案:
func firstDayOfWeek(date: NSDate) -> NSDate {
let calendar = NSCalendar.currentCalendar()
var dateComponents = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitWeekOfMonth, fromDate: date)
dateComponents.weekday = 1
return calendar.dateFromComponents(dateComponents)!
}
答案 6 :(得分:0)
使用NSDateComponents并将weekday
设置为1。
答案 7 :(得分:0)
对于Swift 4.1:
extension Date {
var startOfWeek: Date? {
let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
}
}