我正在寻找一种方法来获得一个月的第一天(在Swift中)。 我想知道它是星期一,星期二等......通过返回相应的数字。
我尝试了许多解决方案,例如获取NSCalendar组件.weekDay但没有人工作。
Example :
print(getTheFirstDate("2016-2-18"))
// Should return : 0 (because the first day of February 2016 is Monday).
任何帮助都会受到赞赏。
答案 0 :(得分:8)
您需要首先将字符串转换为日期,然后您可以从该日期获得月份和年份组件,并使用NSCalendar方法dateFromComponents查找该月的第一天/日期。找到该日期后,您可以从中提取工作日日期组件。试试这样:
Xcode 8.2.1•Swift 3.0.2
extension Date {
var weekday: Int {
return Calendar.current.component(.weekday, from: self)
}
var firstDayOfTheMonth: Date {
return Calendar.current.date(from: Calendar.current.dateComponents([.year,.month], from: self))!
}
}
extension String {
static var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
var date: Date? {
return String.dateFormatter.date(from: self)
}
}
"2016-2-18".date?.firstDayOfTheMonth.weekday // 2 = Monday (Sunday-Saturday 1-7)
extension NSDate {
var weekday: Int {
return NSCalendar.currentCalendar().component(.Weekday, fromDate: self)
}
var firstDayOfTheMonth: NSDate! {
return NSCalendar.currentCalendar().dateFromComponents(NSCalendar.currentCalendar().components([.Year,.Month], fromDate: self))
}
}
extension String {
var date: NSDate? {
struct Date {
static var formatter: NSDateFormatter = {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
}
return Date.formatter.dateFromString(self)
}
}
"2016-2-18".date?.firstDayOfTheMonth.weekday // 2 = Monday (Sunday-Saturday 1-7)
答案 1 :(得分:2)
您可以使用相应的星期几编号:
var weekday = calendar!.component(NSCalendarUnit.Weekday, fromDate: yourNSDate)
var weekdayName = self.getDayOfWeek(weekday)
然后你可以调用我创建的这个函数,根据传入 getDayOfWeek()
的数字得到星期几的相应名称。func getDayOfWeek(weekday:Int) -> String {
if(weekday == 1) {
return "Sunday"
}
else if(weekday == 2) {
return "Monday"
}
else if(weekday == 3) {
return "Tuesday"
}
else if(weekday == 4) {
return "Wednesday"
}
else if(weekday == 5) {
return "Thursday"
}
else if(weekday == 6) {
return "Friday"
}
else {
return "Saturday"
}
}
如果您使用String作为日期开始,可以将其更改为NSDate,如下所示:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /*place date format here*/
let date = dateFormatter.dateFromString(/*your_date_string*/)
答案 2 :(得分:0)
我喜欢Leo的答案,但是如果您想将逻辑封装到单个函数中或者不想使用扩展。
3
编辑:调整代码以利用Leo的伟大建议,并借用他更简洁的创建月份对象的第一个。谢谢,狮子座!
答案 3 :(得分:0)
extension Date {
var startOfWeek: Date {
let gregorian = Calendar(identifier: .gregorian)
let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
return gregorian.date(byAdding: .day, value: 1, to: sunday!)!
}
var endOfWeek: Date {
let gregorian = Calendar(identifier: .gregorian)
let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
return gregorian.date(byAdding: .day, value: 7, to: sunday!)!
}
var startOfPreviousWeek: Date {
let gregorian = Calendar(identifier: .gregorian)
let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
return gregorian.date(byAdding: .day, value: -6, to: sunday!)!
}
var endOfPreviousWeek: Date {
let gregorian = Calendar(identifier: .gregorian)
let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
return gregorian.date(byAdding: .day, value: 0, to: sunday!)!
}
var startDateOfMonth: Date {
return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self)))!
}
var endDateOfMonth: Date {
return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startDateOfMonth)!
}
var getPreviousMonthDate: Date {
return Calendar.current.date(byAdding: .month, value: -1, to: self)!
}
var startDateOfPreviousMonth: Date {
return getPreviousMonthDate.startDateOfMonth
}
var endOfPreviousMonth: Date {
return getPreviousMonthDate.endDateOfMonth
}
}