How to get current time along with the Timezone in XCode? - xcode

I tried this :
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss TZD"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(#"Current Date : %#",dateString);
and the result I get is Date and Time but not the Timezone.

#Vaibhav you have to use time zone like this
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
refer this link
Get the time and date of selected time zone?

Related

Converting string from xml to NSDate and how to translate the date

This is my data string:
<pubDate>Sun, 24 Mar 2013 00:42:13 +0200</pubDate>
I want to convert this to "Sun, 24/3/13, 00:42"
and to translate the day name to hebrew.
here is my code so far:
currentPubDate = [self.xmlParser.pubDate objectAtIndex:indexPath.row];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *loacle = [[NSLocale alloc]initWithLocaleIdentifier:#"he_IL"];
[dateFormatter setLocale:loacle];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
[dateFormatter dateFromString:currentPubDate];
What should I do to make it work? I've read few guides but without success.
Here is the answer for your first part
NSString *currentPubDate = #"Sun, 24 Mar 2013 00:42:13 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:currentPubDate];
[dateFormatter setDateFormat:#"EEE, dd/M/yy HH:mm"];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
For the translation part you have to set locale, but thats not working in my system as well.

NSDateString Conversion

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"W'th week of \n' MMMM YYYY"];
NSString *dateString = [dateFormatter dateFromString:date];
what i want is to output is
1st week of
January 2013
2nd week of
January 2013
3rd week of
January 2013
4th week of
January 2013
1st week of
February 2013'
try this one it'l helps you ,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"W'th week of \n' MMMM YYYY"];
NSLog(#"%#",[dateFormatter stringFromDate:[NSDate date]]);
or
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"W'th week of \n' MMMM YYYY"];
NSLog(#"%#",[dateFormatter stringFromDate:date]); //here is your code went wrong

Get first day in a month from NSCalendar

I have this subset of a method that needs to get day one of the current month.
NSDate *today = [NSDate date]; // returns correctly 28 february 2013
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
dayOneInCurrentMonth then prints out 2013-03-01 09:53:49 +0000, the first day of the next month.
How do I get day one of the current month?
Your logic is wrong: Instead of setting the date's day to 1, you're adding a day to the current date.
Try something like that:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
components.day = 1;
NSDate *dayOneInCurrentMonth = [gregorian dateFromComponents:components];
You want [NSCalender dateFromComponents:] instead:
NSDate *dayOneInCurrentMonth = [gregorian dateFromComponents:components];
Easy way of getting to the 1st from a given date:
NSDate *first = [gregorian dateBySettingUnit:NSCalendarUnitDay value:1 ofDate:date options:0];
Easy Way to get first and last date of previous month is :
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ) fromDate:[NSDate date]];
//TO GET PREVIOUS MONTH LAST DAY
[comp setMonth:[comp month]];
[comp setDay:1];
NSDate *tDateMonth = [gregorian dateFromComponents:comp];
NSLog(#"LAST DAY OF PREVIOUS MONTH ; %#", tDateMonth);
//TO GET PREVIOUS MONTH FIRST DAY
[comp setMonth:[comp month]-1];
[comp setDay:3];
NSDate *td = [gregorian dateFromComponents:comp];
NSLog(#"FIRST DAY OF PREVIOUS MONTH ; %#", td);
Hope this helps
By creating new variable is expensive on memory usage, it's better using date formatter to get the first day of the current month
today = [NSDate date];
firstDayDateFormatter = [[NSDateFormatter alloc] init];
[firstDayDateFormatter setDateFormat:#"01-MM-yyyy"];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
firstDayOfTheMonth = [mdfDateFormat dateFromString:[firstDayDateFormatter stringFromDate:today]];
NSDate *startDate = nil;
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth startDate:&startDate interval:NULL forDate:date];
return startDate;

retaining the time zone in NSDateFormatter

I have to set a format string for a DateFormatter to convert a NSString to a NSDate.
The string is in the format: 2011-01-31 12:45:00 +0200 (y-m-d h:m:s timezone)
I'm using the format: #"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z" , but the timezone is converted to +0000 and the hour is adjusted, so a string like:
2011-01-31 12:45:00 +0200 is converted to:
2011-01-31 10:45:00 +0000
The Code is here:
- (NSDate *) dateForString: (NSString *)dateString
{
NSDateFormatter *dateFormatter;
NSLocale *enUSPOSIXLocale;
NSDate *date;
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"];
// [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
date = [dateFormatter dateFromString:dateString];
return date;
}
This is absolutely expected behaviour when using NSDate - NSDate represents a moment in time so in your case the 2 date/times are equivalent.
Just make sure when you display the date using the a dateFormatter you set the timezone on the date formatter to how you want it displayed.
Have a look at setTimeZone: in
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Some more info can also be found here:
NSDate - Convert Date to GMT (maybe duplicate?)

Time Zone conversion form stringdate

I have a stringdate 16-MAY-2010 23:04:44 which i need to convert to gmt time zone that is the out put required is 17-May-2010 12:03:03.
I used date formatters to convert but the result i am getting is not in the format i required.I am sending the code please let me know if i am doing correct or not
Here is the code:
NSString *timeStamp = [format stringFromDate:[NSDate date]];
NSString *output = [timeConv dateStringFromString:timeStamp];
- (NSString *)dateStringFromString:(NSString *)sourceString
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"dd-MMMM-yyyy HH:MM:ss"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:sourceString];
return [dateFormatter stringFromDate:date];
}
The out put i am getting is the same old time without conversion. so please let me know the correct solution.
I know no cocoa whatsoever, but if I'm understanding your code correctly, you're never telling the parser that your original string is not GMT, but rather GMT-1.
Based on your own code (and again, with no knowledge of cocoa), this is what I believe you need to do:
NSTimeZone *gmtminusone = [NSTimeZone timeZoneForSecondsFromGMT:-3600.0];
[dateFormatter setTimeZone:gmtminusone];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:date];
If it doesn't work right away, maybe it gives you the idea of what to do:
Parse the string with a date formatter set to GMT-1, to output a date.
Parse the date with a date formatter set to GMT, to output a string.
The date string didn't include a time zone specification, so the date formatter relied on you to tell it what time zone it's in.
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:sourceString];
You told it that the input string was in GMT.
You need to tell the date formatter what time zone the date is in. Once you've done that, you can tell the formatter to convert the string to a date (dateFromString:), and it will interpret the string as representing a date in that time zone.
Then, once you have the date so interpreted, switch the formatter's time zone to GMT and have it output the string (stringFromDate:). That string will represent the date converted to GMT.

Resources