tj.:
NSDate *firstDayOfWeek = [[NSDate date] firstDayOfWeek];
Napríklad dnes je 19. augusta, chcel by som získať NSDate
z 2010-08-15 12:00am
z horného riadku kódu. Vďaka!
odpovede:
36 pre odpoveď č. 1Myslím, že toto vlákno reaguje na to, čo hľadáte: http://www.cocoabuilder.com/archive/cocoa/211648-nsdatecomponents-question.html#211826
Pamätajte však, že to nezaberá pondelok ako prvé dni v týždni, takže ho možno budete musieť trochu poklepať odčítaním [gregorian firstWeekday]
namiesto toho 1
, Tiež som to upravil na použitie -currentCalendar
, ale záleží len na vás :-)
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];
21 pre odpoveď č. 2
Prečo tak komplikované? :)
func firstDateOfWeekWithDate(date: NSDate) -> NSDate {
var beginningOfWeek: NSDate?
calendar.rangeOfUnit(.WeekOfYear, startDate: &beginningOfWeek, interval: nil, forDate: date)
return beginningOfWeek!
}
11 pre odpoveď č. 3
Riešenie spoločnosti naixn prinesie zlé výsledky, ak je pondelok (2) nastavený na prvý deň a deň, ktorý kontrolujete, je nedeľa. V takom prípade dostanete výsledok v pondelok budúceho týždňa.
Správnym spôsobom by bolo vypočítať súčty odpočítania takto:
[componentsToSubtract setDay: - ((([weekdayComponents weekday] - [gregorian firstWeekday])
+ 7 ) % 7)];
Za predpokladu, že máte vždy 7 dní v týždni.
2 pre odpoveď № 4
- (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];
}
2 pre odpoveď № 5
To už bolo zodpovedané, ale ako Wolfgang zdôraznil, v prijatej odpovedi je chyba. Ak je prvý deň v týždni nastavený ako väčší dátum ako NSDate
V prípade pochybností bude výsledok nesprávny. Tu je kompletné riešenie, ktoré funguje pre všetky možné firstWeekday
hodnoty (príklad používa pondelok).
- (NSDate *)weekOfDate:(NSDate *)originalDate
{
NSCalendar *currentCal = [NSCalendar currentCalendar];
[currentCal setFirstWeekday:2]; // Sun = 1, Mon = 2, etc.
NSDateComponents *weekdayComponents = [currentCal components:NSCalendarUnitWeekday fromDate:originalDate];
// Calculate the number of days to subtract and create NSDateComponents with them.
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
NSInteger daysToSubtract = (([weekdayComponents weekday] - [currentCal firstWeekday]) + 7) % 7;
[componentsToSubtract setDay:-1 * daysToSubtract];
return [currentCal dateByAddingComponents:componentsToSubtract toDate:originalDate options:0];
}
1 pre odpoveď č. 6
Najjednoduchšie riešenie:
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)!
}
0 pre odpoveď č. 7
použitie NSDateComponents a nastavte weekday
do 1.
0 pre odpoveď č. 8
Pre aplikáciu Swift 4.1:
extension Date {
var startOfWeek: Date? {
let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
}
}