I am trying to convert NSDate to UTC Date and this is what I am using..
NSString* dateString= [NSDate utcStringFromDate:[NSDate date] withFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSZ"];
+ (NSString *)utcStringFromDate:(NSDate *)date withFormat:(NSString *)format{
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[outputFormatter setDateFormat:format];
NSString *timestamp_str = [outputFormatter stringFromDate:date];
[outputFormatter release];
return timestamp_str;
}
This works fine most of the times and gives me a result similar to this.
2013-05-03T05:30:25.18+0000
but at times it gives me a strange value like
2013-04-26T12:03:53 a.m..24+0000
Now I am not sure why it does that..
Thanks for any help.
Related
I want to convert date 2012-12-26 to december 26, 2012 in iOS?
I am using websrvice and the data comes in this format 1990-12-26.
I want to change this to december 26, 2012 format.
This is what I am doing:
lbl_Rightside.text = [rootElement stringValueForNode:#"date"];
NSLog(#"lbl_Rightside is %#",lbl_Rightside.text);
[lbl_Rightside release];
Getting date to this label on 1990-12-26. Now I want to change date to december 26, 2012 format.
Any hints from experts would be very welcome.
you can use NSDateFormatter to do this kind of things. First
convert your date String to a date object using dateFromString:
method.
from date convert to string you want using stringFromDate: method
Different format strings can be found here.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *orignalDate = [dateFormatter dateFromString:YOUR_ORIGINAL_STRING];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *finalString = [dateFormatter stringFromDate:orignalDate];
[dateFormatter release]; //if not using ARC
Check the official Apple documentation about NSDateFormatter. You should use this class to do this kind of formatting.
by using NSDateFormatter
NSString to NSDate
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:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Try to look at NSDateFormatter Class,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; // this is your input date format
NSDate *newDate = [dateFormatter dateFromString:dateString];//converting string to date object
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy"]; // setting new format
NSLog(#"The date is = %#",[dateFormatter stringFromDate:newDate])
I'm trying to convert a string date (2011-06-08T08:05:00.000-08:00) into a NSDate using the following code:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
//Translate 2011-06-08T08:05:00.000-08:00 into 2011-06-08T08:05:00.000-0800
stringDate = [stringDate stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:NSMakeRange([aDate length] - 5, 5)];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:stringDate];
At this point, dateFromString is 2011-06-08 16:05:00 +0000. What I actually wanted was for dateFromString to be 2011-06-08 08:05:00 -0800. What am I doing wrong?
Thanks!
So this will not have a direct answer and will assume that your string format will be the same. What we are going to extract the timezone part (last 5 characters) and then calculate how many seconds we are off from GMT. NSTimeZone has a convenience method timeZoneForSecondsFromGMT: that will help us get what we need. This is what you need to add to the code in the question.
NSString * zoneString = [stringDate substringFromIndex:([stringDate length] - 5)];
NSTimeInterval timeInterval = [[zoneString substringToIndex:3] intValue] * 3600;
timeInterval += [[zoneString substringFromIndex:3] intValue] * 60;
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeInterval]];
[formatter setDateFormat:#"yyyy'-'MM'-'dd' 'HH':'mm':'ss ZZZ"];
NSLog(#"%#", [formatter stringFromDate:dateFromString]);
Hopefully this helps you. If you've found a better answer already, let us know.
Original Answer
One thing about the NSDate is that the time returned is always in GMT. You can't change that. You will have to use an NSDateFormatter to print it right. Something like this,
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"PST"]];
[formatter setTimeStyle:NSDateFormatterFullStyle];
[formatter setDateStyle:NSDateFormatterFullStyle];
NSLog(#"%#", [formatter stringFromDate:[NSDate date]]);
So setting the timezone for the formatter that does dateForString: won't help. You will need to create a new one when you want them with a different timezone.
I'm working on an assignment that allows the user to display events that are happening 'today'. I have parsed the XML file and stored the contents into an array. The contents of the XML file consists of a title, description, date etc. The dates are in NSString format and I want to convert them into NSDates and compare them with today's date before displaying them in a UITableView.
I'm new to obj-c and I've searched online for help on NSDate, but I couldn't find what I need. Any links, advice or help on this is really appreciated. Thanks in advance (:
suppose dateString contains the date in string format
first get date from string:-
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *dateprevious = [formatter dateFromString:dateString];
Now get today date
NSDate *date=[NSDate date];
[formatter setDateFormat:#"dd"];
NSString *dateOfGame =[formatter stringFromDate:dateprevious];
NSString *todaydate =[formatter stringFromDate:date];
[formatter release];
if([todaydate isEqualToString:dateknown])
{
NSLog(#"date matched");
}
Depending on the format of the string, you can use this:
+ (id)dateWithNaturalLanguageString:(NSString *)string
To compare two dates you will find here a lot of usefull answers :)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy , hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [[dateFormatter datefromString:date] retain];
[dateFormatter release];
You can use this one
Have a look at NSDateFormatter
It has a method called dateFromString
Like you could do the following:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *date = [formatter dateFromString:#"5/5/2011"];
I'm trying to format a NSDate to a string with date format of yyyyMMdd, here's the code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date =data.createdTime;
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);
the NSLog return me this value
Tue, 24 May 2011 0:05:01 +0800
(null)
Anyone know which part is wrong?
Thanx in advance.
Perhaps currentArticle.createdTime is a valid date but data.createdTime is nil? In addition, you are performing the -setDateFormat: selector on dateFormat, but it seems like your date formatter is dateFormatter.
Try the following code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyyMMdd"];
NSDate *date = data.createdTime;
NSLog(#" Normal Date = %#", date);
NSLog(#"Formatted Date = %#", [dateFormatter stringFromDate: date]);
[dateFormatter release];
If date is nil, then both NSLog() calls will tell you.
Edit
Double check that data.createdTime is an NSDate instance. Perhaps it is an instance of another class, such as NSString, whose -description returns the displayed date. This would explain why NSLog() “shows” the date, but the formatter is returning nil.
BOOL isDate = [data.createdTime isKindOfClass: [NSDate class]];
NSLog(#"Date %# a date.", isDate ? #"is" : #"is not");
[dateFormat setDateFormat:#"yyyyMMdd"];
Should be:
[dateFormatter setDateFormat:#"yyyyMMdd"];
If it still isn't working then something is wrong with data.createdTime; because I ran this code and recieved the expected output:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date = [NSDate date];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);
I need to convert a date in this string format:
"2011-01-12T14:17:55.043Z"
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
Update: Here is the code I've got so far:
NSString *dateString = #"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
But t comes back as 0 every time.
Use NSDateFormatter to get a NSDate then use - (NSTimeInterval)timeIntervalSince1970 to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2011-01-12T14:17:55.043Z"];
NSLog(#"Date: %#", date);
NSLog(#"1970: %f", [date timeIntervalSince1970]);
NSLog(#"sDate: %#", [formatter stringFromDate:date]);
[formatter release];