In my project I have an NSString that contains data like this
NSString *s=#"20";
I want to convert this as "08:00 ". How can I do this?
I tried this one:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString* dateString = [dateFormatter stringFromDate:#"20"];
but I am getting this dateString as null value.
You input as NSString, need output as NSString.
So intermediate work of NSDate and NSDateFormatter are not required.
You can attain this by :
NSString *s=#"20";
NSString *time=[NSString stringWithFormat:#"%d:00",[s integerValue]%12];
Please try to use this one .I hope it will work fine...
NSString *s=#"20";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH"];
NSDate *date = [df dateFromString:s];
[df setDateFormat:#"hh:mm a"];
NSString *newTime = [df stringFromDate:date];
NSLog(#"Time : %#",newTime);
Related
All,
I want date format in dd/MMM/yyyy format below is the code i am using.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:#"dd/MMM/yyyy"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
But in the dateString i am getting in Oct 21,2014 which is diffrent how can i get in "21/Oct/2014" format please let me know
The issue is that you're calling setDateStyle: instead of setDateFormat:
Use this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MMM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(#"%#", dateString);
This code outputs "22/Oct/2014"
Hi I need to convert this string
"2013-12-05T08:58:55.9345456+02:00"
to NSDate
I am trying next format but without luck
"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"
Code below.
Can you help me with right format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// "2013-12-05T08:58:55.9345456+02:00"
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
Your format is close. You need:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ
And be sure to set the formatter's locale to en_US_POSIX. This should always be done when parsing fixed formatted, non-localized strings.
NSString * dateStr = #"2013-12-05T08:58:55.9345456+02:00";
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // Change here for your formated output
NSDate *date = [df dateFromString:newDateStr];
NSLog(#"%#",date);
Output
2013-12-05 08:58:55 +0000
The problem lies in the timezone where you have +02:00.
The ZZZZ only parses to +0000, hence you are getting nil.
You can do in this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *jsonDateString = #"2013-12-05T08:58:55.9345456+02:00";//#"2013-12-05T08:58:55.9345456+02:00"
NSRange lastColon = [jsonDateString rangeOfString:#":" options:NSBackwardsSearch];
if(lastColon.location != NSNotFound) {
jsonDateString = [jsonDateString stringByReplacingCharactersInRange:lastColon withString: #""];
}
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'.'SZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
NSLog(#"%#",date)
EDIT:
As per rmaddy's comment, you should use SSSSS instead of single S.
This question already has answers here:
Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds
(8 answers)
Closed 9 years ago.
I know there are lot of questions on this topic . But I am not getting a proper solution to my
problem.I am retrieving Date from db , I am getting the Output as
as 2012-07-13 00:00:00.0 .
I am getting the output in the string format . I want the result of type string in the
format MM/dd/yyyy.I am doing the following
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df=[NSDateFormatter new];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *res=[df dateFromString:dateStr];
But here I am getting res as nil .
You need to tell the formatter the whole format of the date, including hours, minutes...
This works:
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df=[NSDateFormatter new];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss.S"];
NSDate *res=[df dateFromString:dateStr];
[df setDateFormat:#"MM/dd/yyyy"];
NSString *myString =[df stringFromDate:res];
Use this one
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss.S"];
NSDate *date = [df dateFromString: dateStr];
[df setDateFormat:#"MM/dd/yyyy"];
NSString *convertedString = [df stringFromDate:date];
NSLog(#"Your String : %#",convertedString);
Output:
Your String : 07/13/2012
try this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.s"];
NSDate *aDate=[formatter dateFromString:[NSString stringWithFormat:#"%#", #"2012-07-13 00:00:00.0"]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"%#", [dateFormatter stringFromDate:aDate]);
Try this
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss.S"];
NSDate *dateObj=[dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *myString = [dateFormatter stringFromDate:dateObj];
NSLog(#"%#",myString);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert NSDate to NSString
convert string to nsdate
Currently I have this code. It's for adding events to the calendar.
[...]
event.startDate = [[NSDate date] dateByAddingTimeInterval:86400];
event.endDate = [[NSDate date] dateByAddingTimeInterval:90000];
What I need is the code to add to a spesific start date and end date, and that's where NSString comes in handy. But I've had no luck converting it so far.
Refer this code :
NSString to NSDate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Hope it helps you
convert NSDate to NSString as
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *string = [dateFormatter stringFromDate:date];
[dateFormatter release];
That's an example if you need the format like “Nov 23, 1937”:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle= NSDateFormatterMediumStyle;
NSString* string= [formatter stringFromDate: date];
Check out the reference for other styles. If you need another style that hasn't a constant, you can use the date format, in this case it's:
formatter.dateFormat= #"MMM dd, yyyy"; // same as medium style
But the preferred way is to use the style, use the format only if there isn't a propert style.
I am reading in a file of strings. Each string looks something like:
!AIVDO,1,1,,B,11b4N?HPs0KLrBDIStg;q?w22<06,0*5A,21/10/2011 12:01:13 PM
What I have done so far is:
NSArray *arrayFromString = [theString componentsSeparatedByString:#","];
NSString *dateString = [tempArrayFromAISString objectAtIndex:7];
// dateString = #"21/10/2011 12:01:13 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
This returns an NSDate of (null).
The string is set ok because when I output it with NSLog it looks good.
The date formatter is ok because if I uncomment the line commented out and set the date string myself it works.
Any ideas as to why the string from the array does not work?
Thanks in advance for any help.
Change to
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss a"];
Your example contains a typo. arrayFromString≠ tempArrayFromAISString
This works for me
NSString *theString = #"!AIVDO,1,1,,B,11b4N?HPs0KLrBDIStg;q?w22<06,0*5A,21/10/2011 12:01:13 PM";
NSArray *arrayFromString = [theString componentsSeparatedByString:#","];
NSString *dateString = [arrayFromString objectAtIndex:7];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"%#", date);
There was a newline character at the end because I was reading from a file. I fixed it with:
dateString = [dateString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
This gets rid of the end of line character.