Change the year, month, and day of the NSDate

in my iPhone application I have a start date for the event, for example: 2013-05-17 15:00:12 +0000. My question is: how can I change 2013-05-17 with today's date, but leave the time the same?

+7
source share
7 answers

You need to assemble the date components and change the necessary properties.

//gather current calendar NSCalendar *calendar = [NSCalendar currentCalendar]; //gather date components from date NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:[NSDate date]]; //set date components [dateComponents setDay:17]; [dateComponents setMonth:5]; [dateComponents setYear:2013]; //save date relative from date NSDate *date = [calendar dateFromComponents:dateComponents]; 

Either this, or you can add the number of seconds in 1 day to increase the value:

 NSDate *date = [NSDate dateWithTimeInterval:((60 * 60) * 24) sinceDate:[NSDate date]]; 
+34
source

Swift 3 and iOS 10.2

  let calendar = Calendar.current var dateComponents: DateComponents? = calendar.dateComponents([.hour, .minute, .second], from: Date()) dateComponents?.day = 17 dateComponents?.month = 5 dateComponents?.year = 2013 let date: Date? = calendar.date(from: dateComponents!) print(date!) 

Swift 3 and iOS 10.2

+4
source

Or fast:

  let calendar = NSCalendar.currentCalendar() let components = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day], fromDate: NSDate.init()) components.day = 17 components.month = 5 components.year = 2013 let date = calendar.dateFromComponents(components) 
+2
source

Read more about my blog https://github.com/onmyway133/blog/issues/54

Today I am trying to change the year of a Date object to 2000 in Swift.

 let date = Date() 

Firstly, I tried with date(bySetting:) , but it does not work from last year. It just returns nil

 Calendar.current.date(bySetting: .year, value: 2000, of: date) 

Secondly, I tried with dateComponents . component.year has changed, but the calendar still returns the original date, very strange !!. No matter what time zone and calendar I use, it still has this problem.

 var component = calendar.dateComponents(in: TimeZone.current, from: base) component.year = year Calendar.current.date(from: component) 

Finally, I tried to be more explicit, and it works 🎉

 var component = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: base) component.year = year Calendar.current.date(from: component) 
+2
source

You need to find numberOfSeconds today at 0am and another dateOfSeconds number at 0am. Find the difference between the two. Then to numberOfSeconds 2013-05-17 15:00:12 +0000 add this difference.

0
source

Why can't you just change the start date with the current date and time you want using the dateWithString API. If not, then you can probably look at the dateByAddingTimeInterval NSDate API and also analyze the time interval between two NSDate

0
source

Swift 3.0
A date-date that has a year, month, and day -5 from the current date

  //CurrentDate let currentDate = Date() let gregorianCalendar = Calendar(identifier: .gregorian) var components = gregorianCalendar.dateComponents([.year, .month, .day], from: currentDate) // Change int value which you want to minus from current Date. components.day = components.day!-5 components.month = components.month!-5 components.year = components.year!-5 let date = gregorianCalendar.date(from: components)! print("Current Date = \(currentDate)") print("Old Date = \(date)") 

Swift 2.0

  //CurrentDate let currentDate = NSDate() let gregorianCalendar = NSCalendar(identifier: .gregorian) var components = gregorianCalendar.dateComponents([.year, .month, .day], from: currentDate) // Change int value which you want to minus from current Date. components.day = components.day!-5 components.month = components.month!-5 components.year = components.year!-5 let date = gregorianCalendar.date(from: components)! print("Current Date = \(currentDate)") print("Old Date = \(date)") 
0
source

All Articles