I want to use the "LLL" string format of NSDateFormatter to get the proper string for the month name: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
All I have is the integer value of the month. For example, if the value is 5, then I need an output string of "May".
I don't want to use a switch. Is there any way I can use NSDateFormatter and apply setDateFormat: with "LLL" ?
A better way to do this is to get the monthSymbols array from your formatter and index into that:
NSString * monthName = [formatter monthSymbols][monthInt];
If you want a date, though, NSCalendar and NSDateComponents will get you there, letting you create an NSDate from whatever date elements you have.
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * comps = [NSDateComponents new];
[comps setMonth:monthVal];
NSDate * monthOnlyDate = [cal dateFromComponents:comps];
Now you can use your formatter on the date.
(Note that this date is basically meaningless aside from its use for this particular purpose; any unset properties -- hours, day, year -- of the components will be undefined, and the calendar will use its own choice of defaults for their values.)
Related
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.
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.
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 = ...
I need to show a date in concrete time zone including DST (European time). App will be used in Lithuania, so time zone is +3 at summer and +2 at other time. The thing is, I have just a list of dates and I don't know how to show +3 for summer dates and +2 for other dates. Currently, I have time zones:
// Eastern European Summer Time UTC + 3 hours
NSTimeZone *timeZoneWithDst = [NSTimeZone timeZoneWithAbbreviation:#"EEST"];
//Eastern European Time UTC + 2 hours
NSTimeZone *timeZoneWithoutDst = [NSTimeZone timeZoneWithAbbreviation:#"EET"];
But how to loop through my list of dates and calculate should I add +3 or +2 to date?
UPDATE Finally I got it working by applying Martin R. suggestion to use time zone by name, not by abbreviation. In this way, date with this time zone handles DST automatically. Here's my code for converting dates:
NSTimeZone *TimeZone = [NSTimeZone timeZoneWithName:#"Europe/Vilnius"];
NSInteger seconds = [myTimeZone secondsFromGMTForDate:someDate];
NSDate *result = [NSDate dateWithTimeInterval:seconds sinceDate:someDate];
To convert an NSDate to a string representation, use NSDateFormatter. By default, it uses the local time zone. To display the date according to a concrete time zone, you can set
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"Europe/Vilnius"];
[dateFormatter setTimeZone:tz];
(According to http://www.timezoneconverter.com/cgi-bin/findzone, the time zone for Lithuania is "Europe/Vilnius".)
This is a very similar alternative that worked for me, in Swift:
var currentDate: NSDate {
let currentLocalTime = NSDate()
let localTimeZone = NSTimeZone.systemTimeZone()
let secondsFromGTM = NSTimeInterval.init(localTimeZone.secondsFromGMT)
let resultDate = NSDate(timeInterval: secondsFromGTM, sinceDate: currentLocalTime)
return resultDate
}
i have a current date using NSDate which is ma start date....and i add 4 more days to the current date where i get ma endDate..
NSString *StrtDate= [dateFormatter stringFromDate:[NSDate date]];
int daysToAdd = 4;
NSDate *newDate1 = [[NSDate date] dateByAddingTimeInterval:60*60*24*daysToAdd];
NSString *StopDate= [dateFormatter stringFromDate:newDate1];
suppose the current date is todays date..thats 28th of jan the end date becomes 32nd of Jan which is invalid rite?
how do u validate the date??
Have you run the code and checked whether the resultant date is indeed 32nd January?
The framework is intelligent enough to understand valid dates. You will get correct date.
You don't need to worry about this as the date framework knows how many days are in each month.