i use this code to get informations from strings with this format "01-05-2011"
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd'-'MM'-'yyyy"];
// Your date represented as a NSDate
NSDate *dateDepart = [formatter dateFromString:daparatureFly.date];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:dateDepart];
Then when i take the month like this [comps day],[comps month] , i have 1 and 5 , i want to have 01, 05 . Any one can help me ? thanx
Those components are NSSintegers, they are not formatted. When you use it in string you can do as follows:
NSString *day = [NSString stringWithFormat:#"%02d", [comps day]];
Something akin to...
//UNTESTED
[NSString stringWithFormat:#"%02d", [comps day]];
Related
I am facing problem while converting given date into system time zone in iOS.
Source date: 2013-03-18 03:54:18 // its in AM format
Destination date should be : 2013-03-18 03:30:15 //its in PM format.
How can I get this??
I have tried below code but getting wrong data and difference too.
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSLog(#"Post date::%#",postDate);
NSDate *utc = [fmt dateFromString:postDate];
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSLog(#" local %#", local);
NSDate *date1 = [NSDate date];
NSString *currentDateString = [fmt stringFromDate:date1];
NSDate *currentDate = [fmt dateFromString:currentDateString];
NSUInteger flags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *difference = [[NSCalendar currentCalendar] components:flags fromDate:utc toDate:currentDate options:0];
NSLog(#"difference::%d",[difference day]);
NSLog(#"difference::%d",[difference hour]);
NSLog(#"difference::%d",[difference minute]);
EDIT: Outputs
Post date::2013-03-18 03:20:09
local 2013-03-18 03:20:09
difference::0
difference::13
difference::2
Replace your code from...
fmt.timeZone = [NSTimeZone systemTimeZone];
as like this
fmt.timeZone = [NSTimeZone localTimeZone];
This will works for you...
NSString *openingHour = #"15:05:35 ";
NSDateFormatter *workingHoursFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[workingHoursFormatter setTimeZone:timeZone];
[workingHoursFormatter setDateFormat:#"HH:mm"];
NSDate *openingTime = [workingHoursFormatter dateFromString:openingHour];
Can you try the above code it works for me , alter the date format to your need
I am new to iPhone developer,
I made a one custom date in that custom date i want to add 1 month or 3 month or 6 month according to frequency.
i want to add month till newDate is less then today's date.
here is my code snippet,
while ([today compare:temp] == NSOrderedDescending)
{
NSLog(#"inside----- today=%#,temp=%#",today,temp);
//add to dates
NSDateComponents *newComponents= [[NSDateComponents alloc] init];
if([FrequencySelText isEqualToString:#"Monthly"]){
[newComponents setMonth:1];
}
if([FrequencySelText isEqualToString:#"Quarterly"]){
[newComponents setMonth:3];
}
if([FrequencySelText isEqualToString:#"Half - Yearly"]){
[newComponents setMonth:6];
}
if([FrequencySelText isEqualToString:#"Yearly"]){
[newComponents setMonth:12];
}
// get a new date by adding components
NSDate* temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSString *newDate = [formatter stringFromDate:temp];
NSLog(#"new date=%#",newDate);
}
My Log shows: inside----- today=2012-07-11 14:41:27 +0000,temp=2012-04-12 03:00:00 +0000
I am getting Exc_bad_access at this line, NSDate* temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0];
Any help will bw appreciated.
Providing value to temp again then no need provide NSDate object type again. Here u already have NSDate *temp then why r u using same name again for NSDate object
temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0]
Maybe try this one:
NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[dateComponents setMonth:1]; // Sample Add
NSDate *tempDate = [gregorian dateByAddingComponents:dateComponents toDate: temp options:0];
NSString *strTempDate = [dateFormat stringFromDate:tempDate];
// New Date on strTempDate
A beginner's problem, but I was wondering if anyone could help me with this:
I need to set four strings according to a string which contains a certain date (e.g. #"Apr 7, 2011"):
a string which will take the day of the week (abbreviated: Mon, Tue, Wed, Thu, Fri, Sat, Sun): e.g. #"Thu"
a string which will take the day, e.g. #"7"
a string which will take the month, e.g. #"April"
and a string which will take the year, e.g. #"2011"
So far, I found this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Output:
// Date for locale en_US: Jan 2, 2001
So this would give me a certain formatted date. I was wondering, however, how I can access certain parts of this date. There is the - (NSArray *)weekdaySymbols but I have no idea how to work this one and the documentation is quite frugal.
Any hints from calendar experts would be very welcome.
EDIT:
I guess this is part of the solution:
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
NSString *gbFormatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMM" options:0 locale:gbLocale];
NSLog(#"gbFormatterString: %#", gbFormatString);
// Output: gbFormatterString: EEE d MMM, e.g. Thu 7 Apr
n.evermind,
You're going to need something like this:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MMM dd, yyy"];
date = [formatter dateFromString:#"Apr 7, 2011"];
NSLog(#"%#", [formatter stringFromDate:date]);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger year = [components year];
NSInteger month=[components month]; // if necessary
NSInteger day = [components day];
NSInteger weekday = [components weekday]; // if necessary
NSDateFormatter *weekDay = [[[NSDateFormatter alloc] init] autorelease];
[weekDay setDateFormat:#"EEE"];
NSDateFormatter *calMonth = [[[NSDateFormatter alloc] init] autorelease];
[calMonth setDateFormat:#"MMMM"];
NSLog(#"%# %i %# %i", [weekDay stringFromDate:date], day, [calMonth stringFromDate:date], year );
output
2011-04-07 12:49:23.519 test[7296:207] Apr 07, 2011
2011-04-07 12:49:23.521 test[7296:207] Thu 7 April 2011
Cheers, Jordan
You should take a Look on NSDateFormatter
#n.evermind
You should actually look at NSCalendar and specifically the - components:fromDate method which gives you the NSDateComponents object which has all the peices you want.
I have two strings both are in following date format (2011-03-22).
i have to compare them and find number of days between them.
Can anyone tell me how to do this..
Please also tell me the correct method to convert them back to NSDate.
Might be a couple of errors as i just typed this, but it speaks for itself and you should get the idea.
NSString *dateStringA = #"2011-03-22";
NSString *dateStringB = #"2011-03-27";
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
df.dateFormat = #"yyyy-MM-dd";
NSDate *dateA = [df dateFromString:dateStringA];
NSDate *dateB = [df dateFromString:dateStringB];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:dateA toDate:dateB options:0];
int daysBetweenDates = comps.day; //This is your days between the 2 dates
NSDate *intervalDate = [[NSCalendar currentCalendar] dateFromComponents:comps]; //This date object represents the duration between them
For string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];
for date to string
NSString *date = [[NSDate date] description];
hi i have 2 dates in string format
base_date_string = 10-12-01 12:00:00
current_date_string = 10-12-23 10:18:00
both the above values are in string
i want to get the number of days elapsed between these 2 dates
I tried to convert them to NSDate using NSDateFormatters and then getting the difference.
I realised that string does not properly converts to NSDate
when i convert to nsdate i got
base_date:::2010-12-01 06:30:00 +0000
current_date::::2010-12-23 04:48:19 +0000 (the time portion is not perfect)
Formatter class that i used is:
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:#"yy-MM-dd HH:mm:ss"];
NSDate *base_date = [formatter1 dateFromString:#"10-12-01 12:00:00"];
[formatter1 release];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"yy-MM-dd HH:mm:ss"];
NSDate *current_date = [formatter2 dateFromString:current_date_string];
[formatter2 release];
//subrtrcation of basedate from current date to get elapsed number of days
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *diff = [calendar components:(NSDayCalendarUnit)
fromDate:base_date toDate:current_date options:0];
int date_value = [diff day];
Please any help is appreciated
30 seconds in the NSDate documentation revealed:
-[NSDate timeIntervalSinceDate:]
So using the dates in your question...
NSTimeInterval difference = [current_date timeIntervalSinceDate:base_date];
difference = fabs(difference);
NSLog(#"there are %f seconds between %# and %#", difference, current_date, base_date);
edit
ok, so the problem is not date differencing. You're observing that the string you're inputting is 5 and 1/2 hours ahead of the date you're getting back.
Well, let's look at this. The date returned is in GMT time (as denoted by the +0000). 5 and 1/2 hours ahead of that is the timezone used in India. So. Are you in India? If you are, then this is just a matter of needing to -setTimezone: on your NSDateFormatter.
You can use this code of function to get the difference between 2 dates
-(int)howManyDaysHavePast:(NSDate*)lastDate :(NSDate*)today {
NSDate *startDate = lastDate;
NSDate *endDate = today;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:startDate
toDate:endDate options:0];
[gregorian release];
int days = [components day];
return days;
}
hAPPY iCODING...
Use the following code
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:#"yy-MM-dd HH:mm:ss"];
NSDate *base_date = [formatter1 dateFromString:#"10-12-01 12:00:00"];
NSDate *current_date = [formatter2 dateFromString:current_date_string];
[formatter1 release];
NSTimeInterval difference = [current_date timeIntervalSinceDate:base_date];
Then you will get difference in number of seconds. Then you can get in number of days as following
float days = difference/86400;
The "days" consists the number of days that the current_date is differ from the base_date.