Unable to convert string date with GMT for Nsdate - ios

I have a string like a below example
2013-12-28T8:15:00+03:00
but i want to convert to NSDate with same format here is my code
NSDateFormatter *dateFormatterInBoundSegment = [[NSDateFormatter alloc] init];
[dateFormatterInBoundSegment setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
[dateFormatterInBoundSegment setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *dateInboundSegment = [dateFormatterInBoundSegment dateFromString:#"2013-12-28T8:15:00+03:00"];
NSLog(#"Formatted date %#",dateInboundSegment);
And result printed like this
Formatted date 2013-12-28 05:15:00 +0000
I want to convert to nsdate with GMT +3 like string ,result should be like this
2013-12-28T8:15:00+03:00

NSDate doesn't store the time zone, NSDate is just a number that represents a date. When you print it on the screen, it's just formatted like that, but your date and that one are the same.
This:
2013-12-28 05:15:00 +0000
and this:
2013-12-28T8:15:00+03:00
Represent the same date, just printed in a different way.
When you do this: NSLog(#"Formatted date %#",dateInboundSegment); You are not specifying how the date has to be formatted to string, so it's using a default mode.
If you want to print the NSDate with the proper timezone, you need to create a string with a formatter and set the timezone.

You are printing the NSDate value, not the formatted string. NSDate doesn't hold any format, and it's printed using the default format.
In order to print a string with your desired format, you must call:
[dateFormatterInBoundSegment stringFromDate:myDate];

Related

Convert NSString to NSDate

I wanted to convet the string to date.
Date in string format is: 2016-09-12T09:52:39Z (Without any space)
Most of the solutions which I found has atleast space in between date and time text. Which is not working in my case.
The major issue in converting above date format is that "T" and "Z". I think some how the date formatter is not distingushing date "dd" and timezone "T". I did attempted to format that but its not working.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:datestring];
Any solution to convert such string into date?
Edit: I had mention while posting question is that - In most of question related to "converting string to date" has spaces in bettwen those text, So, You can write formatter according to that. In my case, There was no space between date and hour part & instead it has T. Hence, I was not able to convert the string date into date object, Instead I was getting null. For which I tried some solutions & after that I posted the question.
plz check your datestring. it's working fine in my end.

NSDateFormatter setDateFormat is not returning value correctly

In my app, I have below code:
if ([fieldValue isKindOfClass:[NSString class]]) {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"MM/dd/yyyy"];
resultValue = [formatter dateFromString:fieldValue];
}
fieldValue is set from a datePicker and let's say the value I got is "2016-03-12 01:13:36 +0000"
In my code, I initialized new NSDateFormatter and called setDateFormat function to set the resultValue in form of "MM/dd/yyyy". For example, 01/13/2016.
However, the value I'm getting for resultValue is also 2016-03-12 01:13:36 +0000.
I'm not sure why this formatter is not working correctly.
You could get the right result as #RyanR described above.
In case you still prefer string as input, you need to tell your formatter the format of the input, and then convert string to NSDate first
let string = "2016-03-12 01:13:36 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss +zzzz"
let date = formatter.dateFromString(string)
Once you have NSDate value, you can convert it to whatever format you like. In your case, if you want to have MM/dd/yyyy, then:
formatter.dateFormat = "MM/dd/yyyy"
let resultValue = formatter.stringFromDate(date!)
This code above give you exactly what you expected
About date format pattern, you could take a look at this link
Well, UIDatePicker has a property named date that you can get the selected NSDate value straight out of.
NSDate *selectedDate = datePicker.date;
Your code example is using a string as the source value, that doesn't make sense within the context of the details that you have provided. Are you sure you aren't trying to format an NSDate object into 'MM/dd/yyyy'? It's very unclear what you are trying to accomplish, but if that is the case you'll want to call -stringFromDate: and pass it your NSDate instance, like so:
NSString *formattedDateString = [formatter stringFromDate:datePicker.date];
You are calling dateFromString. That takes a date STRING the specified format and converts it to an NSDate (an object) An NSDate object does not have an internal format. It is a moment in time, recorded internally as a time in UTF.
Any time you log an NSDate using NSLog (or the debugger "po" or "expression" commands) you will see the date as a string in UTF format, as you show.
If you want the output to be a string, you need to use a date formatter's stringFromDate to convert the date to a string
If you want to convert a date string in one format to another format, you have to convert the date string to an NSDate using one date formatter's dateFromString method, then convert the resulting date to another string using a different date formatter's stringFromDate method

Issues in converting date string (any time zone) to NSDate of French Time Zone

I want to convert a date string (can be in any time zone) to a date in French Time Zone. I am using following code.
NSString * dateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* frenchDateFormatter = [[NSDateFormatter alloc] init];
[frenchDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[frenchDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *frenchDate = [frenchDateFormatter dateFromString:dateString];
NSLog(#"%#",frenchDate);
NSString * frenchString = [frenchDateFormatter stringFromDate:frenchDate];`
Elaboration
--> System time zone is GMT +5
--> French time zone is GMT +2
Date string = 27/05/2015 - 19:00
Expected result = 27/05/2015 - 16:00
Actual result (NSDate) = 2015-05-27 17:00:00 +0000
Actual result (NSString from date) = 27/05/2015 - 19:00
Kindly point out if I am missing something
If you use NSLog to display dates it'll be displayed in UTC. So either you have to convert in your head, or don't use it. I wrote a long answer explaining this to a different question.
Because you have set the timezone of your parsing dateFormatter to Paris the string you parse is treated as "time in paris". That's your problem, you actually wanted to parse it in local time.
The results you get are exactly as one would expect.
You create a NSDate that relates to "19:00 in Paris". Since Paris is UTC+2 that date is 17:00 in UTC (or in +0000). If you convert that date back to "time in Paris" you end up with the same string as before.
If you want to convert the representation of a point in time in your location to a different representation at a different location you have to use two dateFormatters.
NSString *localDateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[localDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *date = [localDateFormatter dateFromString:localDateString]; // date contains point in time. It no longer has a timezone
NSDateFormatter* franceDateFormatter = [[NSDateFormatter alloc] init];
[franceDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[franceDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSString * timeInFranceString = [franceDateFormatter stringFromDate:date]; // representation of the point in time from above for people in Paris
This line prints out the date/time in GMT, as it calls [NSDate description], and there is a potential difference between systemTimeZone and GMT, hence the difference you are seeing:
NSLog(#"%#",currentDate);
If you want to see what the date/time is for a particular timezone then use the NSDateFormatter object to get the string.
A date doesn't have a time zone information. A date is internally represented as a number. We don't have to know anything about that number (it's a number of seconds from a fixed date in UTC), the important thing is to understand that to display a date to a user, you have to convert it to a string first.
A string representation of a number is generated from a date using a date format and a time zone. For all date -> string and string -> date conversions you can use NSDateFormatter.
You have successfully parsed currentDate from your string representation. If you want to reverse the process and get the string representation, just use [currentDateFormatter stringFromDate:currentDate]
Check at http://www.timeanddate.com/worldclock/
Right now Paris is two hours ahead of UTC. The result is absolutely correct. NSDate keeps dates in UTC. The idea is that if any two people look at their watch at the same moment, and convert the time they see on their watch to NSDate, they will get the same result.
You cannot get an NSDate for a timezone. NSDate doesn't support time zones. The only way to get a date with a time zone is to use NSDateFormatter to convert it to a string.

Converting NSString into NSDAte once again

i have a label which is :
_labelCell.text = [2014-06-22 20:27:48 +0000];
What i want to do is to convert this string into NSDate so i can format it into something like : EEEE dd MM yyyy
i try :
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'+0000'"];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(#"Date: %#", dte);
but it always give me a NULL NSDate
Can someone help me on this little thing ?
Thank you very much.
Your date format needs to resemble the format of the date. See http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns for the format patterns. For your date 2014-06-22 20:27:48 +0000 you need to use "yyyy-MM-dd HH:mm:ss ZZZ". Note that it must be "yyyy", not "YYYY", and the zone field should be parsed rather than treated as a literal. There is no "T" separating date and time.
Your date formatter is expecting a T in between the date and time. It returns null because there the string has a space instead of a T.
You're also missing a space before the timezone.
Fix those two issues, and it should work:
dateFormat.dateFormat = #"YYYY-MM-dd HH:mm:ss '+0000'";
Beware this might give you the wrong date, because of time zone issues. Test that out, and if it doesn't work adjust accordingly with dateFormat.timeZone = ...

NSString to NSDate conversion with GMT value

How to set the date formatter setformat option for a date string like this to get NSDate object??
31-07-2012 11:00:00 GMT 05:30
I just need the formatter string to work out on this function OR is there something more to do to get date from this string??
-(void)setDateFormat:(NSString *)string;
Note: I know and worked on the NSDateformatter but cant sort this one out....!!!
I found this blog describes the NSDate and NSString very clear. http://oleb.net/blog/2011/11/working-with-date-and-time-in-cocoa-part-1/. You can refer it.
if the format is
31-07-2012 11:00:00 GMT+05:30
you can use
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ssZZZZ"]

Resources