Get start date from week number - iphone

I've been trying and searching for over two days now.
The only thing I'm trying is to convert a week number "Week 50" to the start date of the week.
I've tried to convert with dateFromString like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"w"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"date(%#)", date);
NSDateFormatter *formatting = [[NSDateFormatter alloc] init];
[formatting setDateFormat:#"YYYY/MM/dd"];
NSString *stringDate = [formatting stringFromDate:date];
NSLog(#"date: %#", stringDate);
[dateFormat release];
But both objects return (null) so it isn't of many help.
Regards

Have you tried the following:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2012];
[comps setWeek:50];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *week50 = [cal dateFromComponents:comps];

You can try trimming the string to just the week number like "50" and then add the desired year number and use following:
NSString *dateStr=#"50 2011";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"w YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"date(%#)", date);

Related

NSDate is not transformed correctly

I am trying to transform a datestring into another format. I am doing it like this.
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/YYYY"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
But this is what I get from my NSLOGs
2013-04-18 08:43:36.181 mosaqua2[9629:907] datestring is NOW 2013-05-04
2013-04-18 08:43:36.184 mosaqua2[9629:907] date transformed 2013-01-03 23:00:00 +0000
2013-04-18 08:43:36.184 mosaqua2[9629:907] datestring is AFTER donderdag, 03/01/2013
What I want it to be is
Saterday, 04/05/2013
Can anybody help me ?
Use my this bellow custom method for convert Date Format ...
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
return convertedString;
}
and use it like bellow...
NSString *strSDate = [self changeDateFormat:_dayObject.p_date dateFormat:#"yyyy-MM-dd" getwithFormat:#"EEEE, dd/MM/yyyy"];
NSLog(#"datestring is AFTER %#",strSDate);
See My Blog with this post...
Check this.....
NSLog(#"datestring is NOW ==>> %#", _dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *date = [dateFormat dateFromString: _dayObject.p_date];
NSLog(#"date transformed ==>> %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER ==>> %#",dateStr);
OUTPUT:~
datestring is NOW ==>> 2013-05-04
date transformed ==>> 2013-05-04 00:00:00 +0000
datestring is AFTER ==>> Saturday, 04/05/2013
Check the character case of parameters
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //MODIFIED
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/yyyy"]; //MODIFIED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
Please try ..this it will work to get the week name
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"eeee"];
NSCalendar * cal = [NSCalendar currentCalendar];
NSUInteger numWeekdays = [cal maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
NSDateComponents * comp = [[NSDateComponents alloc] init];
for( NSUInteger day = 1; day <= numWeekdays; day++ ){
[comp setWeekday:day];
[comp setWeek:0];
NSDate * date = [cal dateFromComponents:comp];
NSString * dayName = [dateFormat stringFromDate:date];
NSLog(#"%#", dayName);
}
You are doing all right except setTimeZone , comment that you will get your output.
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/YYYY"];
//[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]]; //NOT NEEDED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
Change this : "YYYY"
To : "yyyy"
Reason for the error,
It's a common mistake to use YYYY. yyyy specifies the calendar year
whereas YYYY specifies the year (of “Week of Year”), used in the ISO
year-week calendar. In most cases, yyyy and YYYY yield the same
number, however they may be different. Typically you should use the
calendar year.

how to take a date without time in iphone

I am try to remove time from date how to do this i try this code it not working proper where i am wrong
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *todaysDate = [NSDate date];
//NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDateComponents*dateComponents = [gregorian components:NSDayCalendarUnit fromDate:todaysDate];
[dateComponents setDay:1];
app.selectionData.fromDateSelected = [gregorian dateByAddingComponents:dateComponents toDate:todaysDate options:0];
//[dateComponents release];
[gregorian release];
Are you trying to do this?
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int comps = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:comps fromDate:[NSDate date]];
[dateComponents setDay:[dateComponents day] + 1];
app.selectionData.fromDateSelected = [gregorian dateFromComponents:dateComponents];
[gregorian release];
To only get the date, you can use NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSString* currentDate = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
You can also supply your own format for the date.
For ex:
[dateFormattter setDateFormat:#"yyyy-mm-dd"];
You can refer to UTS #35 and Date Formatting Guide for more options on formatting.
Try this out. This works for me
NSDate *todaysDate = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSString *stringDate = [dateFormat stringFromDate:todaysDate];
NSLog(#"stringDate: %#",stringDate);
[dateFormat release];
EDIT:
NSDate *today = [NSDate date];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *date = [today addTimeInterval:secondsPerDay];
//Change NSDate *date = [tomorrow addTimeInterval:-secondsPerDay]; for yesterday
NSDate *tomorrow = date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSString *stringDate = [dateFormat stringFromDate:tomorrow];
NSLog(#"stringDate: %#",stringDate);
[dateFormat release];
Hope this helps you.

convert string into NSDate

i have a "DateTimeArray" this array contain
DateTimeArray[index 0] = 20.05.2011 12:12:50
DateTimeArray[index 1]= 20.05.2011 12:13:20
DateTimeArray[index 2]= 20.05.2011 12:20:10
all the value are in string,and i want to convert this string into NSDate and only want to access time not date
and then this time values will be stored in array and this newly array will be used for drawing line chart
i hope some one know this issue
thank you very much
Code as follows,
for(int index=0;index<[DateTimeArray count];index++)
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss"];
NSString *timeString=[[[DateTimeArray objectAtIndex:index]componentsSeparatedByString:#" "]objectAtIndex:1];
NSDate *time=[df dateFromString:timeString];
[timeArray addObject:time];
[df release];
}
Try this
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss"];
NSDate *formattedDate = [df dateFromString:[DateTimeArray objectAtIndex:requiredIndex]];
[df release];
Best and cleaner approach:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
for(int i=0; i < [DateTimeArray count]; ++i)
{
NSString *string = [DateTimeArray objectAtIndex:i];
NSDate *date = [df dateFromString:string];
[timeArray addObject:time];
}
[df release]; // ALWAYS release unused objects
Then to just access hours, and not days, you should select the required components of each NSDate* instance:
// Get the Gregorian calendar
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Get the date
NSDate* date = [timeArray objectAtIndex:my_index];
// Get the hours, minutes, seconds
NSDateComponents* hour = [cal components:NSHourCalendarUnit fromDate:date];
NSDateComponents* minute = [cal components:NSMinuteCalendarUnit fromDate:date];
NSDateComponents* second = [cal components:NSSecondCalendarUnit fromDate:date];
NSMutableArray *nsdateArray = [NSMutableArray arrayWithCapacity:[DateTimeArray count]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
for(NSString *currentString in DateTimeArray){
NSDate *date = [dateFormat dateFromString:currentString];
[nsdateArray addObject:date];
}
[dateFormat release];
NSLog(#"ArrayWithDate:%#",[nsdateArray description]);
Heres the complete set of code.. Hope it helps..
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
int i=0;//start for{} loop
NSString *currDate = [formatter dateFromString:[DateTimeArray objectAtIndex:i]];
[formatter release];
Happy iCoding...
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"]
NSString *theTime = [timeFormat dateFromString:date];
or
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];

Parse jtring date with objective-c

i have a NSString variables like this
dd-MM-YYYY exemple 30-05-2011 . How can i get day in a variable , month in variable and year in variable ?
thank you
Use NSDateFormatter and NSDateComponents.
In particular, for the format "dd-MM-YYYY":
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd'-'MM'-'yyyy"];
// Your date represented as a NSDate
NSDate *date = [formatter dateFromString:myDateString];
// Now, use NSCalendar / NSDateComponents to get the components
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:date];
Now you're free to access [comps day], [comps month], and [comps year].
See here for a description of all the format-parsing options. Keep in mind that NSDateFormatter also supports older versions of this standard, so you'll have to be careful.
You could do something along the lines of ...
NSString *dateAsString = #"30-05-2011";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"d-MM-yyyy"];
NSDate *datePlain = [formatter dateFromString:dateAsString];
[formatter release];
and then use the NSDate object datePlain
Take a look at the dateFromString: method of NSDateFormatter.
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *d = [df dateFromString:#"2011-05-10"];
[df release];
You can do whatever you need to do with the NSDate object.

Updating time in iphone sdk

I'm using the code provided below to display time and date. can anyone help me with atuomatically changing the time by seconds and the date by the day?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];
You can use NSTimer, specifically scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:. You can use 1 as a time interval.
Use the NSDateComponents class. For example, to add one day and one second to a date:
NSDate *startDate = [NSDate date];
unsigned unitFlags = NSDayCalendarUnit | NSSecondCalendarUnit;
NSCalendar *curr = [NSCalendar currentCalendar];
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
oneDay.day = 1;
oneDay.second = 1;
NSDate* adjustedDate = [curr dateByAddingComponents:oneDay
toDate:startDate
options:0];
Date and Time programming guide