NSDateFormatter is not working with PET format - ios

I want parse date strings from API with time zone PET. So I created NSDateFormatter and converting the string into date but unfortunately its not working. I am getting nil as the result. Any workarounds ?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zz"];
NSDate *newStartDate = [formatter dateFromString:#"Mon Sep 29 14:40:00 2014 PET"];
NSLog(#"newStartDate - %#",newStartDate);

Try with the below function.It will Work...
NSDate *newStartDate = [self dateFromString:#"Mon Sep 29 14:40:00 2014 PET"];
NSLog(#"newStartDate - %#",newStartDate);
- (NSDate *)dateFromString:(NSString*)inDateString
{
NSDateFormatter *pivotalDateFormatter = [[NSDateFormatter alloc] init];
NSDate * dateToBeReturned = nil;
[pivotalDateFormatter setDateFormat:#"EEE MMM dd HH':'mm':'ss yyyy z"];
NSString * timezoneAbbreviation = [[inDateString componentsSeparatedByString:#" "] lastObject];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:timezoneAbbreviation];
if (timeZone)
{
NSString *gmtTime = [inDateString stringByReplacingOccurrencesOfString:timezoneAbbreviation withString:#"GMT"];
dateToBeReturned = [[pivotalDateFormatter dateFromString:gmtTime] dateByAddingTimeInterval:-timeZone.secondsFromGMT];
}
return dateToBeReturned;
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *newStartDate = [NSDate date];
NSString *newStartDateStr = [dateFormatter stringFromDate:newStartDate];
now change the dateformatter dateformat
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zz"]; //set dateformat as per ur requirement
newStartDate = [dateFormatter dateFromString:newStartDateStr];
NSLog(#"%#", newStartDate);
u will get the dateformat u required
happy coding

Related

iOS Date format 28 Mar 2016 / 16:54pm

I want to display the date in the format "28 Mar 2016 / 16:54pm" from the string "2017-05-05T08:27:31.337Z". How can I do this? Is there any buildin method for iOS? Thanks.
use NSDateFormatter for your concept
for e.g
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
NSString *final = [dateFormatter stringFromDate:msgDate];
update
add setAMSymbol and setPMSymbol in your dateFormatter.
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
Full Answer
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
NSString *final = [dateFormatter stringFromDate:msgDate];
See Instruction or introduction with every line
NSString *str = #"your date here..."; /// here this is your date with format yyyyy-MM-dd'T'HH:mm:ss.SSSZ
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);

Date Formatter not working?

I want to change my date format from "MM/dd/YYYY HH:mm:ss" to "EEE dd MMM yyyy hh:mm a", but the code I am using not working for example if my input is 11/30/2016T01:04:30 I am getting the month changed as December, can any one help where is the mistake?
NSString * date = #"11/30/2016T01:04:30";
date = [date stringByReplacingOccurrencesOfString:#"T" withString:#" "];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/YYYY HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [dateFormatter stringFromDate:myDate];
NSLog(#"the converted Day %#",dayName);
no need of this
// date = [date stringByReplacingOccurrencesOfString:#"T" withString:#" "];
use like
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString * date = #"11/30/2016T01:04:30";
[df setDateFormat:#"MM/dd/yyyy'T'HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];
[df setDateFormat:#"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [df stringFromDate:myDate];
NSLog(#"the converted Day %#",dayName);
Output:
the converted Day Wed 30 Nov 2016 01:04 AM

coversion of date and time from 31 May 2016 04:30 PM(current format) to 2016-05-31T16:30:00.000+05:30(Required format)

hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)
NSString *dateString = dateandtime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
used the following code but returns nil
The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.
The input format is dd MMM yyyy hh:mm a
NSString *dateString = #"31 May 2016 04:30 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
Some how I tried and worked
NSString *myString = dateandtime;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
you can use this code.

how to convert 01 Jan 2015 20:33:09 in to 01-02-2015 20:33:09 .it returns nil

I have tried this:
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"%#", myDate);
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy HH:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"%#", myDate);
Your convert date nil because you didn't use right dateFormat
try :
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss"];
more infomation
For your required Format,Use this:-
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:myDateString];
dateFormatter.dateFormat = #"dd-MM-yyyy HH:mm:ss";
NSString *myDate = [dateFormatter stringFromDate:date];
NSLog(#"%#",myDate);
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss"];
NSDate *myDate = [dateFormatter dateFromString:myDateString];
//here i get correct date ok But
NSLog(#"%#", myDate);
NSString *ss=[NSString stringWithFormat:#"%#",myDate] ;
NSArray *components = [ss componentsSeparatedByString: #"+"];
NSString *StartnewDateString = [components objectAtIndex:0];
//Here time is changed of StartnewDateString

Convert NSString to NSDate and back to different format NSString

I need to take the NSString Dec 4, 2012, 12:33 PM and convert it to separate out the month, day, and year, so that I can have 3 different strings of 12, 04, and 2012.
I figure that I should convert the NSString to NSDate and then reformat the date to change out NSString, but am running into issues.
I have:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:substring];
NSLog(#"Date%#", dateFromString);
[dateFormatter release];
However, the date keeps coming back null.
The problem is with your locale I think.
You should try to print how your dateFormatter formats current date [NSDate new].
It works for me:
NSString* substring = #"Dec 12 2012 12:08 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d yyyy h:mm a"]; // not 'p' but 'a'
NSDate *dateFromString = [dateFormatter dateFromString:substring];
Convert
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
to, Because MMM dd, yyyy, hh:mm p not a valid date formate
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm a"];
And
NSDate *dateFromString = [dateFormatter dateFromString:substring];
To get string again
NSLog(#"%#",[dateFormatter stringFromDate:dateFromString]);
for me NSLog is Dec 04, 2012, 12:33 PM
#import "NSString+Date.h"
#implementation NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
{
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];
return dateFromString;
}
+(NSString*)StringFromDate :(NSDate*)date;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
return stringDate;
}
` #end

Resources