从一周中返回日期的日期不在星期

从一周中返回日期的日期不在星期

本文介绍了从一周中返回日期的日期不在星期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很奇怪的bug。当使用此方法获取一年的一周时间:

  let dates = NSMutableArray()
let cal = NSCalendar.currentCalendar()
cal.firstWeekday = 2
let formatter = NSDateFormatter()
formatter.dateFormat =ww YYYY
formatter.calendar = cal
let date = formatter.dateFromString(week as String)
println(date)

字符串 52 2014 ,所以预计的日期是12月22日星期一,而是返回12月20日星期六23:00。首先,我以为我会通过设置日历的 firstWeekday -option来处理一周的第一天,但​​没有运气。另外,在第52周,返回的日期不是均匀的。



只需检查一下,我跑了$ code> cal.components(NSCalendarUnit.WeekOfYearCalendarUnit,fromDate :date!)。weekOfYear 双重检查我不是一个白痴,没有先生,生产日期的一周是51,在所需的一周之前的一周。



任何想法如何达到预期的结果?

解决方案

将第一个星期日从1更改为2不会更改日期,只会从星期日到星期一的第一个工作日。



您可以这样做:

  func dateFromWeekOfYear(year:Int,weekOfYear:Int,weekday:Int) - > NSDate {
return NSCalendar.currentCalendar()。dateWithEra(1,yearForWeekOfYear:year,weekOfYear:weekOfYear,weekday:weekday,hour:0,minute:0,second:0,nanosecond:0)!
}

let date1 = dateFromWeekOfYear(2014,52,1)// 2014年12月21日,12:00 AM
let date2 = dateFromWeekOfYear(2014,52,2) // 2014年12月22日,12:00 AM
let date3 = dateFromWeekOfYear(2014,52,3)// 2014年12月23日,12:00 AM
pre>

如果处理一个字符串,并且想要设置他独立当地的星期几,你可以这样做:

  let myDate =2 52 2014
let cal = NSCalendar.currentCalendar()
let formatter = NSDateFormatter()
formatter。 dateFormat =c ww Y
formatter.calendar = cal
如果让date1 = formatter.dateFromString(myDate){
date1 //2014年12月22日,12:00 AM
}

如果您需要进一步的参考,可以使用:




I have come across a rather strange "bug". When getting a date for a week of a year using this method:

let dates = NSMutableArray()
let cal = NSCalendar.currentCalendar()
cal.firstWeekday = 2
let formatter = NSDateFormatter()
formatter.dateFormat = "ww YYYY"
formatter.calendar = cal
let date = formatter.dateFromString(week as String)
println(date)

The string week is 52 2014, so the expected date would be Monday December 22th, but instead it returns Saturday December 20th, at 23:00. First of all, I thought I'd handled the first day of week by setting the firstWeekday-option of the calendar, but no luck. In addition, the date returned isn't even in week 52.

Just to double check I ran cal.components(NSCalendarUnit.WeekOfYearCalendarUnit, fromDate: date!).weekOfYear to double check I'm not an idiot, and no sir, the week for the date produced is 51, the week before the desired week.

Any idea how I can reach the expected result?

解决方案

Changing the firstWeekday from 1 to 2 won't change the date, it will change just the First weekday from Sunday to Monday.

You can do it like this:

func dateFromWeekOfYear(year:Int, weekOfYear:Int, weekday:Int) -> NSDate {
    return NSCalendar.currentCalendar().dateWithEra(1, yearForWeekOfYear: year, weekOfYear: weekOfYear, weekday: weekday, hour: 0, minute: 0, second: 0, nanosecond: 0)!
}

let date1 = dateFromWeekOfYear(2014, 52, 1)   // Dec 21, 2014, 12:00 AM
let date2 = dateFromWeekOfYear(2014, 52, 2)   // Dec 22, 2014, 12:00 AM
let date3 = dateFromWeekOfYear(2014, 52, 3)   // Dec 23, 2014, 12:00 AM

If dealing with a string and you want to set he Stand Alone local day of week you can do it like this:

let myDate = "2 52 2014"
let cal = NSCalendar.currentCalendar()
let formatter = NSDateFormatter()
formatter.dateFormat = "c ww Y"
formatter.calendar = cal
if let date1 = formatter.dateFromString(myDate) {
    date1  // "Dec 22, 2014, 12:00 AM"
}

If you need further reference you can use this:

这篇关于从一周中返回日期的日期不在星期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:15