Convert date to next day's date in iOS - objective-c

I have a date in string format just like 30-11-2012. I want the date next to it like 01-12-2012 again in string format.
Is there any way of getting it?

You should use NSCalendar for best compatibility
NSDate *today = [NSDate dateWithNaturalLanguageString:#"2011-11-30"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 1;
today = [calendar dateByAddingComponents:dayComponent toDate:today options:0];
//2011-12-01 12:00:00 +0000

I got the answer by someone I forget the name, I was about to accept his/her answer but when I clicked on accept answer it told me that the post bas been removed.
The answer given by that anonymous person was given below
NSString *dateString = #"22-11-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDateComponents *components= [[NSDateComponents alloc] init];
[components setDay:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *dateIncremented= [calendar dateByAddingComponents:components toDate:dateFromString options:0];
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringFromDate = [myDateFormatter stringFromDate:dateIncremented];
NSLog(#"%#", stringFromDate);
In the end I really like to thanks that anonymous person, I know this was not as much tricky but this helped me a lot. Thanks again you the Anonymous Helper.

swift 3.0
var components = DateComponents()
components.day = 1
let now = Date()
let futureDate = Calendar.current.date(byAdding: components, to: now, wrappingComponents: false)
//now = 2017-03-22 futureDate = 2017-03-23

Related

Return tomorrow's day name

I would like to return the weekday name of tomorrow (i.e. if today is Sunday, I want Monday). Here's my code that yields today's weekday name.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
weekDay = [formatter stringFromDate:[NSDate date]];
Rather than messing with NSCalendar, what would be a concise way of doing this (following the code I have here)?
Thank you.
user's language aware:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
NSArray *weekdays = [df weekdaySymbols];
NSDateComponents *c = [[NSDateComponents alloc] init];
c.day = 1;
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:c
toDate:[NSDate date]
options:0];
c = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit
fromDate:tomorrow];
NSString *tomorrowname = weekdays[c.weekday-1];// the value of c.weekday may
// range from 1 (sunday) to 7 (saturday)
NSLog(#"%#", tomorrowname);
if you need to have the name in a certain language, add
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
after the creation of the date formatter.
Why are you afraid of "messing" with NSCalendar? For using the NSDateComponents methods, you will actually have to make use of NSCalendar. This is my solution for your problem.
// Get the current weekday
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// !! Sunday = 1
NSInteger weekday = [weekdayComponents weekday];
Now, you can use the NSDateFormatter method -(NSArray *)weekdaySymbols which contains weekdays starting with Sunday at index 0. As the integer returned by [weekdayComponents weekday] starts with 0 for Saturday, we do not have to increase the value stored in weekday:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// !! Sunday = 0
NSUInteger symbolIndex = (weekday < 7) ? weekday : 0;
NSString *weekdaySymbol = [[formatter weekdaySymbols] objectAtIndex:(NSUInteger)symbolIndex];
I hope this was helpful although using NSCalendar to some extent.
EDIT: glektrik's solution is pretty straight ahead. But mind the following statement in the NSDate class reference of - dateByAddingTimeInterval:.
Return Value:
A new NSDate object that is set to seconds seconds relative to the receiver. The date returned might have a representation different from the receiver’s.
Thank you for your comment, Abizern. Here's what I did:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
weekDay = [formatter stringFromDate:[NSDate date]];
NSDateComponents *tomorrowComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSDate *compDate = [[NSCalendar currentCalendar] dateFromComponents:tomorrowComponents];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
offsetComponents.day = 1;
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:compDate options:0];
nextWeekDay = [formatter stringFromDate:tomorrow];
Two NSString objects (weekDay and nextWeekDay) now store the days of the week names for today and tomorrow (currently Sunday and Monday).
This works well, but I wonder if there's an easier way. Objective-C dates are quite cumbersome :(
Thanks again.

how to convert localized Date String to NSDate

I have NSString *localized_Date = minutesSinceMidnightToLocalizedTime (time units)
I get this string as 4:30 am, 10.00pm
how do I convert this string to NSDate?
PLease let me know.
I know it's a bit long code, but I wanted to make it as readable as possible.
This code uses minutesSinceMidnight instead of the localized string.
NSDate *currentDate = [NSDate date];
NSDateComponents *currentDateComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:currentDate];
NSDateComponents *currentDateToMidnight = [[NSDateComponents alloc] init];
[currentDateToMidnight setHour:-[currentDateComponents hour]];
[currentDateToMidnight setMinute:-[currentDateComponents minute]];
[currentDateToMidnight setSecond:-[currentDateComponents second]];
NSDate *midnight = [calendar dateByAddingComponents:currentDateToMidnight toDate:currentDate options:0];
NSDateComponents *midnightToDesiredDate = [[NSDateComponents alloc] init];
[midnightToDesiredDate setMinute:minutesSinceMidnight];
NSDate *desiredDate = [calendar dateByAddingComponents:midnightToDesiredDate toDate:midnight options:0];
NSDateFormatter is your friend, use the formats that suits your date/time
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:[NSString stringWithFormat:#"dd MMM yyyy HH:mm"]];
NSDate *_date = [df dateFromString:string];
Johan
Try this code:
NSString *dateString = #"4:30 am";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormatter dateFromString:dateString];
Hope this will help you.

How to get day of month in number format?

What I'm looking for is the day of the month, example 2, 5, 30 or 31. In integer form, and not a string. I'm not looking for a programmed date, but today's date in whatever local they are in.
All the above answers are bad ways to accomplish this. This is the right way:
NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:date];
NSInteger day = components.day;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d"];
NSInteger day = [[dateFormat stringFromDate:[NSDate date]] intValue];
Start with an NSDate then run it through an NSDateFormatter.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Here is something that may work :
NSDate *theDate;
// Set theDate to the date you want
// For example, theDate = [NSDate date]; to get today's date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"d";
NSString *theDayString = [formatter stringFromDate:theDate];
int theDay = theDayString.intValue;
NSLog (#"%d", theDay);
theDay contains the value you're looking for !

Date Format as 2012-12-20T16:00:00.000Z?

I'm getting date as 2012-12-24 08:59:00 +0000 and i want it as 2012-12-20T16:00:00.000Z
i tried with this code
NSDateComponents *dayComponent = [[NSDateComponents alloc] init] ;
NSDate* now = [NSDate date];
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDateFormatter* formatter = [NSDateFormatter alloc];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
dayComponent.day = -2;
NSDate* fromDate = [theCalendar dateByAddingComponents:dayComponent toDate:now options:0];
fromDateStr = [formatter stringFromDate:fromDate];
my requirement is to get date of previous days like before 2 days, before 14 days etc in specified format.
but i'm getting fromDateStr as empty string.
Any suggestion would be appreciated.
The date format for 2012-12-20T16:00:00.000Z is
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
Hope that helps.
Let me know if that does not give you the desired result.
Here is a sample code that worked for me:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *str = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date = %#",str);
The output was:
Date = 2012-12-26T14:55:36.702Z

How to get today's date in the Gregorian format when phone calendar is non-Gregorian?

NSDate *now = [[NSDate alloc] init];
gives the current date.
However if the phone calendar is not Gregorian (on the emulator there is also Japanese and Buddhist), the current date will not be Gregorian.
The question now is how to convert to a Gregorian date or make sure it will be in the Gregorian format from the beginning. This is crucial for some server communication.
Thank you!
NSDate just represents a point in time and has no format in itself.
To format an NSDate to e.g. a string, you should use NSDateFormatter. It has a calendar property, and if you set this property to an instance of a Gregorian calendar, the outputted format will match a Gregorian style.
NSDate *now = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:gregorianCalendar];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterFullStyle];
NSString *formattedDate = [formatter stringFromDate:now];
NSLog(#"%#", formattedDate);
[gregorianCalendar release];
[formatter release];
The picked answer actually I can't compare them. only display is not enough for my project.
I finally come up with a solution that covert (NSDate) currentDate -> gregorianDate
then we can compare those NSDates.
Just remember that the NSDates should be used temporary .(it do not attach with any calendar)
NSDate* currentDate = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *gregorianComponents = [gregorianCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:[gregorianComponents day]];
[comps setMonth:[gregorianComponents month]];
[comps setYear:[gregorianComponents year]];
[comps setHour:[gregorianComponents hour]];
[comps setMinute:[gregorianComponents minute]];
[comps setSecond:[gregorianComponents second]];
NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *today = [currentCalendar dateFromComponents:comps];