Issue in converting the date format using NSDateFormatter - ios

All,
In My project i want to convert date from MM-dd-yyyy to M/d/YY (12/31/15) but i am getting wrong format, below is the code i am using.
-(NSString*)dateConversionForWarranty:(NSString *)string
{
string i am getting as 12-31-2014
NSDateFormatter* df_utc = [[NSDateFormatter alloc] init];
[df_utc setDateFormat:#"MM-dd-yyyy"];
NSDate *date = [df_utc dateFromString:string];
When i am converting it i am getting date as this "2014-12-30 18:30:00 +0000"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"M/d/YY"];
Then when i try to convert to above format i am getting the year completely wrong "12/31/15"
NSString *formattedDate = [dateFormatter stringFromDate:date];
return formattedDate;
}
Can any one tell me where i am wrong

Don't use capital "Y" -- it produces a very unpredictable value for dates near the start/end of the year (look it up). You can use a single "M" if you wish, to suppress leading zeros in the month.
In summary:
d - day of month
M - month
y - year
h - hour -- 12 hour clock
H - hour -- 24 hour clock
m - minute
s - second
S - fraction of a second
a - AM/PM
The number of repeats corresponds to the field width (though not always 1:1). Eg "MMMM" will give you the full-length spelled-out month name, while "MMM" gives the 3-letter abbreviation and "MM" gives the 2-digit numeric month. Refer to the spec (highlighted above) for details.

i want date in this format 12/31/15 then which type of formatter
try this [dateFormatter setDateFormat:#"MM/dd/yy"];.
For more info about the Date Formatting patterns

Do it in below way..
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm"];
// or
[dateFormat setDateFormat:#"mm/dd/yyyy HH:mm"];

Related

NSDate - dateFromString is returning nil

I have an NSString that has a date. I'm trying to convert that date to an NSDate. When I do that I get nil fro the NSDate. Here is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"dateString = %# date = %#", dateString, date);
Here is the output of the NSLog:
dateString = 2015-06-16 date = (null)
What am I doing wrong, and how can I fix it?
The date format does not match the date string. It needs to be: :#"yyyy-MM-dd"
The order and other characters need to match.
yyyy for a four digit year
MM for a two digit month
dd for a two digit day
See: ICU Formatting Dates and Times
Note: NSLog() uses the NSDate description method which presents date/time referenced to GMT (UTC) and NSDateFormatter defaults to your timezone so the date displayed may be different.

Why does my NSDate (only time part) string format not able to convert to long?

Am taking input from UIDatePicker and my output is in this format
3:24:00 AM but when i tried to convert this back to NSDate and from there long its returning a null value.
My code:
NSDate* sourceDate = self.timePicker.date;
NSString *dateString = [NSDateFormatter localizedStringFromDate:sourceDate
dateStyle:NSDateFormatterNoneStyle
timeStyle:NSDateFormatterMediumStyle];
NSLog(#"%#",dateString); //3:25:00 AM string value
Converting dateString to NSDate
NSDate *dateVal=(NSDate *)dateString;
NSLog(#"%#", dateVal); // printing null in console
converting dateVal to long
long val=[d timeIntervalSince1970]*1000;
NSLog(#"%ld",val); // printing 0
How do i solve this issue? please help me
Iam expecting a long value for time part of NSDate
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
This will help you to Convert dateString to NSDate
You need the following :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
[df setDateFormat:#"h:m:s a"];
NSDate *myDate = [df dateFromString:dateString];
You need to convert a string to an NSDate object. A simple way to do this is using the NSDateFormatter object. It provides the dateFromString method which converts a string into an NSDate object. You do, however, need to tell NSDateFormatter the format the date will be in.
Date formatter reference :
a AM/PM
A millisecond of day
c day of week (c,cc,ccc,cccc)
d day of month
e day of week (e,EEE,EEEE)
F week of month
g julian day (since 1/1/4713 BC)
G era designator (G=GGG,GGGG)
h hour (1-12, zero padded)
H hour (0-23, zero padded)
L month of year (L,LL,LLL,LLLL)
m minute of hour (0-59, zero padded)
M month of year (M,MM,MMM,MMMM)
Q quarter of year (Q,QQ,QQQ,QQQQ)
s seconds of minute (0-59, zero padded)
S fraction of second
u zero padded year
v general timezone (v=vvv,vvvv)
w week of year (0-53, zero padded)
y year (y,yy,yyyy)
z specific timezone (z=zzz,zzzz)
Z timezone offset +0000
sql y-M-d H:m:s
rss [E, ]d MMM y[y] H:m:s Z|z[zzz]

Trying to get the current date as just month, day and year

I'm using the folowing code to get the date as a string
strStartDate.text=[mOnTimeApp->cDate descriptionWithLocale:nil];
It works but has the hour, minute and second. Is there a way to just get the Month, day and year as a string?
Passing nil into descriptionWithLocale: is the same as calling description on an instance of NSDate. NSDate Class Reference has the following to say about descriptionWithLocale:
Return Value
A string representation of the receiver, using the given locale, or if
the locale argument is nil, in the international format YYYY-MM-DD
HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours
and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”)
You can configure an instance of NSDateFormatter to represent the desired format and then use the method stringFromDate: passing in your NSDate object -
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
strStartDate.text = [formatter stringFromDate:mOnTimeApp->cDate];
Is this why you are looking for?
+ (NSString*)getAbsoluteDateAndTimeFromString:(NSNumber*)formattedTime
{
long long ts = [formattedTime longLongValue];
ts /= 1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:ts];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"M/d/yyyy h:mm a"];
return[dateFormatter stringFromDate:date];
}

Problems converting a string date to NSDate

I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);

NSDateFormatter difference

What is the difference between these two Date Formats. First one give actual time but second on give time buy adding time zone offset value.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"Date: %#",dateConverted); //
Date: 2013-12-02 12:15:43 +0000
NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter1 setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
NSDate *dateConverted1 = [dateFormatter1 dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"%#",dateConverted1);
Date: 2013-12-02 06:45:43 +0000
The problem with the 2nd format is all of the needless quotes, especially around the Z. By quoting the Z this means the Z is treated as a literal character and not the timezone format specifier.
Get rid of the quotes around the Z and both will give the same result.
The second date formatter is incorrect, the 'Z' should not be single quoted, that keeps it from being considered a format character.
Also the only single quotes that are needed are around the 'T' so that is is not considered a format character but rather a literal.
See ICU User Guide: Formatting Dates and Times

Resources