Parse jtring date with objective-c - iphone

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.

Related

Get time from NSDate returns nil

I am trying to show time on graph and i have a full time stamp in this #"2012-08-28 18:50:24" format. when i try to get time from this date then it returns nil in NSDate.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
in this code only if I change [formatter setDateFormat:#"HH:mm:ss"]; line to [formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; then it starts working with full date and time. But i need only time
u have string . so you must have to convert it into date first as per your string format. after that you can convert date into any format. so follow this.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *str = [formatter stringfromdate:time];
NSLog(#"%#",str);
NSDateFormatter is used to format your NSDate to your requirements.
Here you are fetching the date from a string so you have to tell the NSDateFormatter to look for the string with that format and convert it into date
NSDate contains both date and time so if you want the time only to show use dateformatter to get the time component from NSDate
iOS manages time and date together in NSDate.Actually they are supposed to work together.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
// GET Proper NSDate
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
//To your requirement
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timestr=[formatter stringFromDate:time];
NSLog(#"%#",timestr);
Because, first you need to convert your string into NSDate in the right formate that is
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:#"2012-08-28 18:50:24"];
Now you can reformate the NSDateFormatter according to your need. Now you can get time from the
[formatter setDateFormat:#"HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:aDate];
If you need to produce Date from String then you'll need to set the date format accordingly.
This is important that you create a NSDate object first! Only then you can separate time from whole date.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
then
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:time];
You can also use other string concatenation functions to remove the Date Modules like this.
However, if you have an NSDate first, then your previous code would work.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:date];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh:mm:ss a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
First you have converted your string date and time in the date formate..
YourTimeStr = #"2012-08-28 18:50:24";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-mm-dd HH:mm:ss";
NSDate *DateAndTime = [formatter dateFromString:LogOutTimeSTR];
then
NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSString *YourTimeStr =[dateFormatter1 stringFromDate:DateAndTime];

Objective-C String(yyyy-mm-dd) to NSDate

My App is a JSON code which involves a date in string type. (Such as "2011-10-01").
I would like to know how I could conver it into NSDate?
It needs to be displayed in a different time format such as "1 October, 2011".
ex. this code doesn't work:
NSString *date1 = #"2010-11-12";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MMMM-dd"];
NSDate *date2 = [dateFormat dateFromString:date1];
NSString *strdate = [dateFormat stringFromDate:date2];
As Tug writes in his blog post on the subject:
Objective-C and iOS SDK provide a class to help formatting date
(marshaling and unmarshaling), this class is NSDateFormatter. No
surprise, the NSDateFormatter uses the Unicode Date Format
Patterns.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:publicationDate ];
[dateFormatter release];
where publicationDate in this case is an NSString.
Use NSDateFormatter. The appropriate method is dateFromString:. Take a look at the documentation :)
You can try this
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd MM yyyy"];
NSString *todayString = [df stringFromDate:[NSDate date]];
NSDate *someDate=[NSDate date];
NSString *targetDateString = [df stringFromDate:someDate];
NSLog(#"date:%#",targetDateString);

Get start date from week number

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);

How to specify the NSDate Format in Iphone app?

I am trying to make a date format in such a manner hours.minutes day/month/year
I have uses this code :
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
NSDate *dateAndtime = [NSDate date];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
//[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString* dateforBD=[[NSString alloc]init];
dateforBD =[formatter stringFromDate:dateAndtime];
But this code not give me my format.
Also i have tried this code:
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy"
options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
It's also give me different format. How can i achieve the required format means 13.10 21/4/2011
All the formats return in month before date. May i have used wrong way for this ?
Thanks in advance.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];
To explore more visit http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
Hope it helps.
for date before month use -
[dateFormat setDateFormat:#"dd/MM/yyyy hh:mm"];
As specified in Apple's NSDateFormatter reference, the NSDateFormatter uses Unicode Date Format Patterns.
According to the unicode patterns, you would need the following pattern to achieve "13.10 21/4/2011":
hh:mm dd/MM/yyyy
So you would use this code:
NSDate *dateAndtime = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm dd/MM/yyyy"];
NSString *newlyFormattedDateString = [dateFormatter stringFromDate:dateAndtime];

iphone NSDate - get todays date and force the time

hopefully a quick one for someone! I am struggling to see why the code below isn't working. I am trying to get today's date (eg: 2010-09-10) and manually adding the time. It always seems to return 00:00:00 for the time though. any ideas where I'm going wrong?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSString *actualDate = [dateFormatter stringFromDate:[NSDate date]];
NSDate *sDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# %#", actualDate, #"01:00:00"]];
NSDate *eDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# %#", actualDate, #"23:59:59"]];
The NSDate objects are returning the format 2010-09-10 00:00:00 +01:00 all the time. Somewhere it's just not picking up the time.... any ideas? many thanks.
* UPDATE *
The code below works. I did as suggested and accessed the elements and updated them.
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"yyyy-MM-dd hh:mm:ss";
//update for the start date
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSDate *sDate = [calendar dateFromComponents:comps];
//update for the end date
[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];
NSDate *eDate = [calendar dateFromComponents:comps];
You should use NSCalendar instead then. It allows you to parse the current date and modify its components. Like you can initialize it with today's date and then set the time and then get an NSDate from it.
(Old answer: Because your formatter is configured to only recognize the date part. Add the time part to it (even though its name is NSDateFormatter, it will happily parse dates and times) and you will see the time too.)
I found this question today while looking to do the same thing, create an NSDate with a specific time today. The solution presented wasn't quite what I needed so I came up with this:
NSDate * date;
NSString * string, *timestamp;
NSDateFormatter * formatter;
timestamp = #"17:30";
formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: #"yyyy-MM-dd "];
[formatter setTimeZone: [NSTimeZone localTimeZone]];
string = [formatter stringFromDate: [NSDate date]];
string = [string stringByAppendingString: timestamp];
[formatter setDateFormat: #"yyyy-MM-dd HH:mm"];
date = [formatter dateFromString: string];