Date that is one year from another date - ios

I'm trying to calculate the date that is a year from the current date:
let calendar = NSCalendar.currentCalendar()
let currDate = NSDate()
let newDate = calendar.dateBySettingUnit(.Year, value: 2017, ofDate: currDate, options: NSCalendarOptions(rawValue: 0))!
But newDate is always January 1, 2017.

You got it almost
let calendar = NSCalendar.currentCalendar()
let currDate = NSDate()
let newDate = calendar.dateByAddingUnit(.Year, value: 1, toDate: currDate, options: [])

Related

How to set weekdays date in CollectionView starting from Monday to Saturday in swift

I have to show weekdays date in CollectionView start from Monday to Saturday. Until the end of the week, I have to show that weekdays date only. My task image Image. Please help/advise me how to do this task.
I am getting weekdays but its start from current day, But i need start date from monday,
func arrayOfDates() -> NSArray {
let numberOfDays: Int = 6
let formatter: DateFormatter = DateFormatter()
formatter.dateFormat = "dd"
let startDate = Date()
let calendar = Calendar.current
var offset = DateComponents()
var dates: [Any] = [formatter.string(from: startDate)]
for i in 1..<numberOfDays {
offset.day = i
let nextDay: Date? = calendar.date(byAdding: offset, to: startDate)
let nextDayString = formatter.string(from: nextDay!)
dates.append(nextDayString)
}
return dates as NSArray
}
Try this
func formattedDaysInThisWeekNet() -> [String]
{
// create calendar
let calendar = NSCalendar(identifier: NSCalendar.Identifier.gregorian)!
// today's date
let today = NSDate()
let weekday = calendar.component(.weekday, from: today as Date)
let beginningOfWeek : NSDate
if weekday != 2 { // if today is not Monday, get back
beginningOfWeek = calendar.nextDate(after: today as Date, matching: .weekday, value: 1, options: [.matchNextTime, .searchBackwards])! as NSDate
} else { // today is Monday
beginningOfWeek = calendar.startOfDay(for: today as Date) as NSDate
}
var formattedDays = [String]()
for i in 0..<7 {
let date = calendar.date(byAdding: .day, value: i, to: beginningOfWeek as Date, options: [])!
formattedDays.append(formatDate(date: date as NSDate))
let firstDate = calendar.date(byAdding: .day, value: 0, to: beginningOfWeek as Date, options: [])!
let lastDate = calendar.date(byAdding: .day, value: 6, to: beginningOfWeek as Date, options: [])!
let fullString = "\(formatDateFull(date: firstDate as NSDate)) - \(formatDateFull(date: lastDate as NSDate))" as String
fulldateLbl.text = "< \(fullString) >"
print(fullString)
}
return formattedDays
}
enum Days: Int {
case Sat = 0, Sun, Mon, Tue, Wed, Thu, Fri
static var all = [Mon, Tue, Wed, Thu, Fri, Sat]
}
func getWeekDays(date: Date) -> [Date] {
var weekDates = [Date]()
let cal = Calendar.current
var comps = cal.dateComponents([.weekOfYear, .yearForWeekOfYear], from: date)
let days = Days.all
for day in days {
comps.weekday = day.rawValue
weekDates.append(cal.date(from: comps)!)
}
return weekDates
}
print(getWeekDays(date: Date())) // print all dates from Monday to Saturday
Hi guys thank you for your response. I completed This task.
func arrayOfDates() -> NSArray {
var calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let today = calendar.startOfDay(for: Date())
let dayOfWeek = calendar.component(.weekday, from: today) - calendar.firstWeekday
let weekdays = calendar.range(of: .weekday, in: .weekOfYear, for: today)!
let dayss = (weekdays.lowerBound ..< weekdays.upperBound)
.compactMap { calendar.date(byAdding: .day, value: $0 - dayOfWeek, to: today) }
//.filter { !calendar.isDateInWeekend($0) }
let formatter = DateFormatter()
formatter.dateFormat = "dd"
let strings = dayss.map { formatter.string(from: $0) }
self.dates = strings as NSArray
return dates as NSArray
}
But here I have one question, How to disable the previous dates. I need to select only current date and next dates.

Date not getting correctly using NSDate In Swift

I want to print date [1,2,3.... current date] but not getting correct result.
Because when I set first day is "1" in my code, the output in [2,3,4]. And when I set day is "0". It result show correct but show an extra date [1,2,3,4,5]. And today is 4th October 2016, as my time zone.
let date = NSDate()
let components = NSCalendar.currentCalendar().components([.Day , .Month , .Year], fromDate: date)
let year = components.year
let month = components.month
let day = 1 // Output is [2,3,4]
// let day = 0 than o/p [1,2,3,4,5]
dateFormatatter.dateFormat = "MMMM yyyy"
monthNameLabel.text = dateFormatatter.stringFromDate(date)
startDate.year = year
startDate.month = month
startDate.day = day
let startDateNSDate = calendar.dateFromComponents(startDate)!
var dateStart = startDateNSDate // first date
let endDate = NSDate() // last date
dateFormatatter.dateFormat = "dd"
while dateStart.compare(endDate) != .OrderedDescending {
// print(fmt.stringFromDate(date))
// Advance by one day:
dateStart = calendar.dateByAddingUnit(.Day, value: 1, toDate: dateStart, options: [])!
let dateFormat1 = NSDateFormatter()
dateFormat1.dateFormat = "dd-MM-yyyy"
dateArrayCalendar.addObject(dateFormatatter.stringFromDate(dateStart))
dateArrayForCompare.addObject(dateFormat1.stringFromDate(dateStart))
}
And I want result like this [1,2,3,4]
And Same issue Here
let componentsForCompare = calendar.components([.Year, .Month], fromDate: date)
let startOfMonth = calendar.dateFromComponents(componentsForCompare)!
print(startOfMonth)//2016-09-30 18:30:00 +0000
print(dateFormatatter.stringFromDate(startOfMonth)) //01
Its give different Outputs
You need to increment your date at the end of the while loop, rather than at the start:
while dateStart.compare(endDate) != .OrderedDescending {
// print(fmt.stringFromDate(date))
// Advance by one day:
let dateFormat1 = NSDateFormatter()
dateFormat1.dateFormat = "dd-MM-yyyy"
dateArrayCalendar.addObject(dateFormatatter.stringFromDate(dateStart))
dateArrayForCompare.addObject(dateFormat1.stringFromDate(dateStart))
dateStart = calendar.dateByAddingUnit(.Day, value: 1, toDate: dateStart, options: [])!
}

How to get the number of days of current/last year

I want to find out the number of days from current and last year. I have managed to come up with this code which will give me a starting and ending date.
Then I use NSDateComponents to get the number of days for this current year. But what I get is 365, which means is the number of days from last year.
How can I corectly get the number of days for last and current year ?
func numOfDaysInYear() {
let calendar = NSCalendar.currentCalendar()
var interval = NSTimeInterval(0)
var startOfYear: NSDate?
var endOfYear: NSDate!
calendar.rangeOfUnit(.Year, startDate: &startOfYear, interval: &interval, forDate: NSDate())
endOfYear = startOfYear?.dateByAddingTimeInterval(interval - 1)
let comp = calendar.components(.Day, fromDate: startOfYear!, toDate: endOfYear, options: .MatchFirst)
print(comp.day)
}
endOfYear = startOfYear?.dateByAddingTimeInterval(interval - 1)
gives a date within the current year, and its difference to
startOfYear is 365 days, 23 hours and 59 seconds, and therefore the result is 365.
What you want is
let startOfNextYear = startOfYear!.dateByAddingTimeInterval(interval)
let comp = calendar.components(.Day, fromDate: startOfYear!, toDate: startOfNextYear, options: .MatchFirst)
print(comp.day)
which gives the correct output 366 for the leap year 2016.
To get results for the previous year, just replace the current data
NSDate() by a date "one year ago":
calendar.dateByAddingUnit(.Year, value: -1, toDate: NSDate(), options: [])
Alternative approach
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.day = 1
components.month = 1
let nextFirstJanuar = calendar.nextDateAfterDate(NSDate(), matchingComponents: components, options: .MatchNextTime)!
let thisFirstJanuar = calendar.nextDateAfterDate(nextFirstJanuar, matchingComponents: components, options: [.MatchNextTime, .SearchBackwards])!
let lastFirstJanuar = calendar.nextDateAfterDate(thisFirstJanuar, matchingComponents: components, options: [.MatchNextTime, .SearchBackwards])!
let daysInThisYear = calendar.components(.Day, fromDate: thisFirstJanuar, toDate: nextFirstJanuar, options: []).day
let daysInLastYear = calendar.components(.Day, fromDate: lastFirstJanuar, toDate: thisFirstJanuar, options: []).day

NSDate returning wrong date in Swift

Today is Feb 1 of 2016 and I just opened up my working app to continue my work and this shocks me.
It is a collection view of date, I didn't notice anything wrong during January because it hasn't been a month.
Here's the code,
func dF(someDate:NSDate) -> (String){
let monthFormat = NSDateFormatter()
let dayFormat = NSDateFormatter()
monthFormat.dateFormat = "MMM"
dayFormat.dateFormat = "D"
let aMonth = monthFormat.stringFromDate(someDate)
let aDay = dayFormat.stringFromDate(someDate)
//concatenate strings
let conString = aMonth + " " + aDay
return (conString)
}
//from today to 7th day, to be used in ScrollView
let day1 = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: 0, toDate: NSDate(), options: [])
let day2 = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -1, toDate: NSDate(), options: [])
let day3 = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: [])
let day4 = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -3, toDate: NSDate(), options: [])
let day5 = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -4, toDate: NSDate(), options: [])
let day6 = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -5, toDate: NSDate(), options: [])
let day7 = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -6, toDate: NSDate(), options: [])
let titles = [dF(day1!), dF(day2!), dF(day3!), dF(day4!), dF(day5!), dF(day6!), dF(day7!)]
I'm expecting day1 to pass the current date to function dF and format it.
So I ran some checks to make sure I've got the correct date
Current time: Feb 1, 2016, 1:08am in my time zone
print(day1)
print(day2)
print(ddd)
Here's the result from console:
Optional(2016-01-31 17:08:47 +0000)
Optional(2016-01-30 17:08:47 +0000)
Feb 32
I'm suspecting my NSDateComponent isn't setup correctly/on par with local time zone. Any ideas?
You are using the wrong specifier for the day of the month.
You are creating your date string the hardest way possible.
Try this:
Swift 3/4:
func dF(_ someDate: Date) -> String {
let format = DateFormatter()
format.dateFormat = "MMM d"
// For a more localized format, use the following instead:
// format.setLocalizedDateFormatFromTemplate("MMMd")
let conString = format.string(from: someDate)
return conString
}
Old Swift 2 answer:
func dF(someDate:NSDate) -> (String){
let format = NSDateFormatter()
format.dateFormat = "MMM d"
let conString = format.stringFromDate(someDate)
return (conString)
}
More Swifty way to solve your problem. Works with Swift 3 and above.
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMM d"
let result = formatter.string(from: date)
Output
"May 12"

How to get next 10 days from current date in swift

I have dateformat string like this "2015-03-09".How do i get next 10 days date from current date?any help will be appreciated.thanks in advance
For a purely Swift 3 solution:
Calendar.current.date(byAdding: .day, value: 10, to: Date())
Here's how to get a date 10 days from now, using built-in date modification method .dateByAddingUnit
If all 10 days (dates) are required, can be looped by "value:10" part and added to array.
var tenDaysfromNow: NSDate {
return NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: 10, toDate: NSDate(), options: [])!
}
print(tenDaysfromNow)
And for Swift3:
var tenDaysfromNow: Date {
return (Calendar.current as NSCalendar).date(byAdding: .day, value: 10, to: Date(), options: [])!
}
You can use the dateByAddingTimeInterval() method for this.
var dateStr = "2015-03-09"
var formatter = NSDateFormatter()
formatter.dateFormat = "YYYY-MM-dd"
var currDate = formatter.dateFromString(dateStr)
for i in 1...10
{
var interval = NSTimeInterval(60 * 60 * 24 * i)
var newDate = currDate?.dateByAddingTimeInterval(interval)
println(newDate)
}
EDIT:
As mentioned by #Martin R in the comments, it'll be better to use dateByAddingComponents() of NSCalendar class:
var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
var dateComponent = NSDateComponents()
for i in 1...10
{
dateComponent.day = i
var newDate = calendar?.dateByAddingComponents(dateComponent, toDate: currDate!, options:nil)
println(newDate)
}
There is a function 'dateByAddingTimeInterval()' for an NSDate object. With this, you can create a NSDate from your date string. Then add 10 days = 10*24*60*60 to get next 10 days NSDate value
let today : NSDate = ....
let next10days = today.dateByAddingTimeInterval(10*60*60*24); //interval = seconds
//then you convert back to your date string format if you want, by using NSDateFormatter
To avoid problem with Daylighttime saving (#MartinR):
let cal = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let next10Days = cal.dateByAddingUnit(NSCalendarUnit.DayCalendarUnit, value: 10, toDate: today, options: nil)
Here is a swift 4/5 version of the answer by #MidhunMP
var dateStr = "2015-03-09"
var formatter = DateFormatter()
formatter.dateFormat = "YYYY-MM-dd"
var calendar = Calendar(identifier: .gregorian)
var dateComponent = DateComponents()
if let currDate = formatter.date(from: dateStr) {
for i in 1...10 {
let newDate = calendar.date(byAdding: .day, value: i, to: currDate)
print(newDate)
}
}
For Swift 3:
let now = NSDate()
let plusTen = now.dateByAddingDays(10)

Resources