Date Format - time zone - ios

I receive from the server a string with a date that looks like this: "2013-03-29T15:27:00Z". How can I format it and transform it into an NSDate?

Try this:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [formatter dateFromString:#"2013-03-29T15:27:00Z"];

NSString *departTimeDate = #"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [dateFormatter dateFromString:departTimeDate];
NSLog(#"Expected Result___ %#",date);

NSString *string=#"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date=[dateFormatter dateFromString:string];

Related

objective-c dateFromString returns nil on specific date

The date is always nil, but if I change the dateString to any other date like 03/01/1975 it works normally. Any idea?
NSString *dateString = #"05/01/1975";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"us"];
[dateFormatter setLocale:gbLocale];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
Try this
NSString *dateString = #"05/01/1975";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"MM.dd.yyyy"];
NSDate *anotherDate = [formatter dateFromString:dateString];
NSLog(#"%#",anotherDate);
It will give output like this
1975-05-01 00:00:00 +0000
try this code
- (NSDate *)getPickerDateForString:(NSString *)dateString
{
NSDate * dt;
if (!dateFormatter)
{
dateFormatter = [[NSDateFormatter alloc] init];
}
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dt = [dateFormatter dateFromString:dateString];
return dt;
}

While converting NSString to NSDate?

I try to convert NSString to NSDate with this code:
NSString *dateString=[[NSString alloc] init];
dateString = [[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date = dateFromString;
But msg.Date is nil even if dateString shows the correct date string. What am I missing?
change this format
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
into and try
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
Update
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
// use simple
msg.date = [dateFormatter dateFromString:#"2016/05/06 12:36"];
NSLog(#"final date == %#",dateFromString);
//NSDate *dateFromString = [dateFormatter dateFromString:#"2016/05/06 12:36"];
// NSLog(#"final date == %#",dateFromString);
Try this code to convert NSString to NSDate
NSString *dateString = #"2016-05-06";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Use this Code,
NSString *dateString=[[NSString alloc] init];
dateString =[[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date=dateFromString;
hope its helpful

Convert date string(2016-04-27T08:06:07.531Z) to specific date format dd.MM.YYYY

I have date string in the format 2016-04-27T08:06:07.531Z and want to convert into 2016.04.27. I have tried following code
NSString *dateString = #"2016-04-27T08:06:07.531Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
NSDate *dateReceived = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"dd.MM.YYYY"];
NSString *dateReq = [dateFormatter stringFromDate:dateReceived];
NSLog(#"Date Req:%#",dateReq);
I am getting null in console for NSLog. Any help will be appreciated. Thanks.
use like
NSString *dateString = #"2016-04-27T08:06:07.531Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateReceived = [dateFormatter dateFromString:dateString];
// 2016.04.27
[dateFormatter setDateFormat:#"yyyy.MM.dd"];
NSString *dateReq = [dateFormatter stringFromDate:dateReceived];
NSLog(#"Date Req:%#",dateReq);
Plz just change your date formatter's date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
instead of
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
try this it will help you --
NSString *date = #"2011-02-27T08:06:07.531Z";
NSDateFormatter *dFormat = [[NSDateFormatter alloc] init];
[dFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dReceive = [dFormat dateFromString:dateString];
[dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
NSString *dateReq = [dateFormatter stringFromDate:dReceive];
just change date format
[dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
Use date format as: [dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];

Which dateFormat should I choose while converting NSString to NSDate?

I have a NSString like #"2014-11-27T10:54:08.185Z"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate * date = [dateFormatter dateFromString:self.creationTime];
I tried a lot of different formatter strings, but the date is always nil.
Or is there another problem here?
The hour in your date string format seems to be in the 24h format.
So you need to use HH instead of hh in your dateFormat.
More info
Try
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.creationTime];
self.dateLabel.text = [formatter stringFromDate:date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Please try this...:)

Not getting correct date from string

I am using following code to get a date from string but its not showing correct date
NSDateFormatter *saveddf = [[NSDateFormatter alloc] init];
[saveddf setDateFormat:#"EEEE_MMMM dd_YYYY hh:mm a"];
[saveddf setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *savedtdate = [[NSDate alloc] init];
savedtdate = [saveddf dateFromString:datestring];
Saturday_June 22_2013 11:44 AM -- INPUT (datestring).
2013-01-05 06:14:00 +0000 This is output i am getting.(savedtdate).
Please tell whats wrong in my code.
Try this code, it should work:
NSString *yourStringDate = #"Saturday_June 22_2013 11:44 AM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE_MMM dd_yyyy hh:mm a"];
NSDate *date = [dateFormatter dateFromString: yourStringDate];
//Set the required date format
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
Try to use this code
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSLog(#"%#",dateString);

Resources