I have a string:
stringValue = 2013-06-11 06:01:28.
When i try to convert it using NSDateFormatter it becomes like this: date = 2013-06-10 22:01:28 +0000.
I've read that they are the same point in time. In fact when i get the string value of the date, i get the string above.
If they're the same, is there a way to have a date with value equal to my string above? Can i have an NSDate *date = 2013-06-11 06:01:28? If yes, how can I do that?
It happen because when you convert string to date it convert to GMT time format
if you want date same as your string value than you have to set your stadard local time
Like
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
[timeFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss'"];
date = [timeFormat dateFromString:#"2013-06-11 06:01:28"];
hope it may help you
Use this formatter:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
Related
So i am getting a string containing date and time in this format "2014-12-22T11:00:00+0500" Now in order to convert it into NSdate i am using
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:start_time];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString* temp = [dateFormatter stringFromDate:date];
self.eventDate = [dateFormatter dateFromString:temp];
NSDateFormatter* timeFormatter = [[NSDateFormatter alloc]init];
[timeFormatter setDateFormat:#"HH:mm:ss"];
NSString* temp2 = [timeFormatter stringFromDate:date];
self.start_time = [timeFormatter dateFromString:temp2];
Now even though the conversion is successful the problem is that eventDate also has has time after date 00:00:00. How can i remove this so that eventDate only contains date.
Conversly start_time has the time of event but also has some arbritrary reference date before that. How can i remove that so i only have time in start_time
I have searched hard and fast but haven't been able to figure out this problem. Any help would be appreciated.
You cannot remove either the date or the time to keep only one component. If I remember correctly NSDate object is internally just a number of seconds relative to a fixed point in time. So every NSDate contains the full date and time information.
What you probably want to do is to get the NSDateComponents you want from a NSDate object.
Instead of trying to store this separate, just display these dates separate. I think it could be useful sometimes to get the date completly, but i don't know your idea.
You can try with it, it may be help you.
NSString *finalDate = #"2014-12-22T11:00:00+0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dateFormatter dateFromString:finalDate];
//For getting Time
NSDateFormatter* df1 = [[NSDateFormatter alloc]init];
[df1 setDateFormat:#"hh:mm:ss"];
NSString *time = [df1 stringFromDate:date];
NSLog(#"time %# ", time);
//For getting Date
NSDateFormatter* df2 = [[NSDateFormatter alloc]init];
[df2 setDateFormat:#"yyyy-MM-dd"];
NSString *actualDate = [df2 stringFromDate:date];
NSLog(#"actualDate %# ", actualDate);
I would like to format a date to display in a uitableview custom cell
The data is passed into the app from a CMS - it is provided as a string in the following format and stored in a date type variable-
2014-04-15 10:10:45 +0000
Our app will initially be UK based - so I need to convert the format into DD/MM/YYYY format.
I tried the following code to parse my date (dateadded which is of type date).
NSDateFormatter *formatter = [NSDateFormatter new];
cellRecP.artDate.text = [formatter stringFromDate:resItem.dateadded];
but this just returns null - i guess the date format provided above is not anything that stringfromdate understands - is there any other way to format date?
Use an 'NSDateFormatter'
NSDateFormatter* newFormatter = [[[NSDateFormatter alloc] init] autorelease];
with this format to parse the string
[newFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss zzz"];
NSDate* aDate = [newFormatter dateFromString:dateString];
and you should get a valid 'NSDate' object
Try This and also Checkout this All Formate It is really good and helpful.
NSString *yourString = #"2014-04-15 10:10:45 +0000";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];//Set Your Timezone
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];//You have to set this formate.
NSDate *dd = [df dateFromString:yourString];//This will convert into date;
//Now you can set your formate.
[df setDateFormat:#"dd/MM/yyyy"];
NSString *str = [df stringFromDate:dd];
You need an NSDateFormatter and a proper unicode parse string. NSDateFormatter by default automatically checks the device locale setting the correct output.
This is an example from some code of mine:
NSDateFormatter *dateWriter = [[NSDateFormatter alloc] init];
dateWriter.dateFormat = #"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'ZZZ";
dateWriter.dateStyle = NSDateFormatterMediumStyle;
dateWriter.timeStyle = NSDateFormatterMediumStyle;
Pay attention that date formatters are pretty expensive to create.
The following code will set date to nil.
NSString *dateString = #"2014-04-27T04:20:07.000-04:00";
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
NSDate *date = [formatter dateFromString:dateString];
What am I doing wrong?
I've tried many other different variations for UTC_FORMAT, but counldn't seem to get it. I'm also a little bit confused as to when and where the single quotes go. After playing with this for a while, I'm assuming it can goes around characters that shouldn't be interpreted by the formatter, but that's a separate thing.
Related Links That Couldn't Help Me:
Apple Docs: Data Formatting Guid
SO: Why is NSDateFormatter returning nil?
Formats That I've Tried:
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSString *UTC_FORMAT = #"yyyy-MM-dd'T'HH:mm:ss-Z";
Your date looks like a quite standard JSON date format in RFC3339 format. However, there are several possibilities how these dates can be formatted. In this case, your date string contains milliseconds. Your date format doesn't, so this cannot work. The following code will check for dates without fractional seconds first, then for dates with fractional seconds. Furthermore, you are looking for a literal character Z instead of a timezone.
The "X5" is documented at
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
and converts time zones in quite a flexible way, including the colon in the middle. .SSSSSS will convert fractional parts of seconds up to microseconds. Should you be given nanoseconds change it to nine S characters.
And I forgot the locale information...
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ssX5";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
gmtTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setLocale:enUSPOSIXLocale];
[formatter setTimeZone:gmtTimeZone];
NSDate *date = [formatter dateFromString:dateString];
if (date == nil)
{
NSString *UTC_FORMAT2 = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSSSSX5";
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:UTC_FORMAT2];
[formatter2 setLocale:enUSPOSIXLocale];
[formatter2 setTimeZone:gmtTimeZone];
date = [formatter2 dateFromString:dateString];
}
To avoid dependencies on the current locale, add :
NSString *dateString = #"2014-04-27T04:20:07.000-04:00";
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [formatter dateFromString:dateString];
I want to convert a string to an NSDate, like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterFullStyle];
NSDate *createdAt = [dateFormat dateFromString:[value valueForKey:#"created_at"]];
When I log createdAt it returns nil for some reason and I can't figure out why.
[value valueForKey:#"created_at"]];
Returns a NSCFString that looks like this:
2013-05-15T05:55:57Z
I have no idea why it's nil, any help would be appreciated!
You should set date format to the NSDateFormatter first. This should be working:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *createdAt = [dateFormat dateFromString:[value valueForKey:#"created_at"]];
However I'm not sure about your input date string. The last letter Z should be identification of date's time zone. Letter Z is the format specifier for the time zone according to Unicode Date Format Patterns (http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns).
So I have a date string I receive that looks like this: "2013-03-20T21:13:26-7:00" that I receive from a web back end. I have no control over the back end, just a fyi.
My preference would be to have the date formatted like this: 9:13pm at 3/20.
When I do the following
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date = [dateFormatter dateFromString:#"2013-03-20T21:13:26-7:00"];
date is null.
My first thought is that the date string looks odd, and maybe I should remove the T and the "-7:00", as the "-7:00" is appended to every date I receive, and I'm not sure what it is for.
Even after the string looks like #"2013-03-20 21:13:26", date is still null.
I will admit I am not a pro at formatting dates, so if I could get some help with this issue, that would be great.
Thanks in advance!
You have to set dateFormat to the dateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
NSDate *date = [dateFormatter dateFromString:#"2013-03-20T21:13:26-7:00"];
[dateFormatter setDateFormat:#"hh:mma 'at' MM/yy"];
NSString *dateString = [dateFormatter stringFromDate:date];
Set the date format for the dateFormatter, your problem lies in the last part of the date, secondly you can set the T in the dateformatter as follows
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *date = [dateFormatter dateFromString:#"2013-03-20T21:13:26-7:00"];
Set dateFormat,
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];