I have dateString with this format "mm/dd/yyyy '-' HH:mm".
I want to extract the date in string and hour in string.
I've tried the following code but this return a wrong date:
- (void) extractDate: (NSString *)dateString intoHour: (NSString *)hourLabel and: (NSString*)dateLabel {
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"mm/dd/yyyy '-' HH:mm"];
NSData *date = [formatter dateFromString:dateString];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:date];
hourLabel = [NSString stringWithFormat:#"%lu:%lu", [components hour], [components minute]];
dateLabel = [NSString stringWithFormat:#"%lu/%lu/%lu", [components day], [components month], [components year]];
}
Exemple String: "05/01/2015 - 11:15"
Result: date: 1/1/2015, time: 11:15
and also how can I maintain this date format dd/mm/yyyy, I mean when the day is 1, it shows 01
Not tested but:
MM for the month. Not mm which is minutes.
You already have a NSDateFormatter so why not use it again instead of NSDateComponents and NSString formats?
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy '-' HH:mm"];
NSDate *date = [formatter dateFromString:dateString];
//Now you have the correct date. So we'll use the NSDateFormatter to transform NSDate (date) into NSString according to the format we need.
[formatter setDateFormat:#"MM/dd/yyyy"];
dateLabel = [formatter stringFromDate:date];
[formatter setDateFormat:#"HH:mm"];
hourLabel = [formatter stringFromDate:date];
hourLabel/dateLabel as NSString and not UILabel is quite a misleading var naming.
Related
i want to get the current date in format like this 24 February 2014 . i want
also get Islamic date in the same format as 5 jumada al-awwal 1436
how can i do that i saw other thing but most care about integer components.
i wrote this code which just get components
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSLog(#"%#",components);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
i used this to Islamic date
NSCalendar *islamicCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
NSDate *today = [NSDate date];
[islamicCalendar rangeOfUnit:NSDayCalendarUnit startDate:&today interval:NULL forDate:today];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar:islamicCalendar];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
//english output
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSString * islamicDateString = [dateFormatter stringFromDate:today];
NSLog(#"%#", islamicDateString);
I have following code to convert string to date. Is it possible that it sets time as "00:00:00" and not the current time?
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd"];
NSString *str = #"2014-08-08";
NSDate *dt = [dateformat dateFromString:str];
This gives dt as "2014-08-08 15:20:00 +0000" because I did the operation at 15:20.
Edit: I am using this date to convert it to integer later to store it in database:
int t = [dt timeIntervalSince1970];
If you are displaying the date dt with NSLog you will see what the date description method provides. If you want to see the date in a specific way that suits you use NSDateFormatter to format the date.
Example:
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd"];
NSString *str = #"2014-08-08";
NSDate *dt = [dateformat dateFromString:str];
NSDateFormatter *displayDateformat = [[NSDateFormatter alloc] init];
[displayDateformat setDateFormat:#"yyyy-MM-dd"];
NSString *displayDateString = [displayDateformat stringFromDate:dt];
NSLog(#"displayDateString: %#", displayDateString);
Output:
2014-08-08
Note per Apple docs: "This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT."
A good practice is to use NSDateComponents
NSDate *yourDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:yourDate];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
// Set the time components manually
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
yourDate = [calendar dateFromComponents:dateComponents];
Update
iOS8 :
[[NSCalendar currentCalendar] startOfDayForDate:[NSDate date]];
I have 2 dates. Date1 in yyyy-MM-dd 21:00:00 +0000. Date2 in yyyy-MM-dd
I need to compare it and get age result.
Problem is that Ia have an error in NSDateComponents string.
NSDate *date1 = [NSDate date];
NSDate *date2temp = uBirthdate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:date2temp];
[dateString stringByAppendingString:#" 21:00:00 +0000"];
NSDate *date2 = [dateFormatter dateFromString:dateString];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:date2 toDate:date1 options:0];
NSInteger editedAge = components.year;
NSLog(#"%d ", editedAge);
It will be better to cut off '21:00:00 +0000' from date1, but I do not know how can I do it
In short words I plan to get current dateTime, change the time and make it local to Malaysia Time by applying +0800 to timezone.
The result is unexpected :
-(NSDate *)departureDateTime
{
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: 7];
[components setMinute: 59];
[components setSecond: 17];
NSDate *newDate = [gregorian dateFromComponents: components];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-mm-dd HH:mm:ss Z"];
[dateFormat setLocale:[NSLocale currentLocale]];
NSString *newDateString = [NSString stringWithFormat:#"%#",newDate];
NSString *maskString = [NSString stringWithFormat:#"%#", [newDateString substringToIndex:20]];
NSString *append = [maskString stringByAppendingString:#"+0800"];
NSLog(#"%#",append);
NSDate *finalLocalDate = [dateFormat dateFromString:append];
return finalLocalDate;
}
Results :
for NSLog Append : 2013-12-07 23:59:17 +0800
but finalLocalDate : 2013-01-07 15:59:17 +0000
Found the answer with much shorter solution, so I posted here in case it helps anyone in future.
for returning, the problem was different time zones so by adding this line of
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ];
we set the timezone to system time zone then we remove unnecessary codes :
-(NSDate *)departureDateTime
{
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ];
[components setHour: 7];
[components setMinute: 59];
[components setSecond: 17];
NSDate *newDate = [gregorian dateFromComponents: components];
NSLog(#"%#",newDate);
return newDate;
}
Correct Result : 2013-12-08 07:59:17 +0000
try this:
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
NSDate *malaysianTime = [NSDate dateWithTimeIntervalSince1970:now+(8*60*60)]; //8h in seconds
If your computer is running in the correct timezone don't set a timezone.
Create your newDate value and then --
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString* printableDate = [fmt stringFromDate:newDate];
NSLog(#"The date is %#", printableDate);
Only set a timezone (in both NSCalendar and NSDateFormatter) if the desired timezone is not the one your computer/phone is currently using.
Note that if you NSLog newDate directly it will print in GMT timezone. This is the way it's supposed to be -- you always use NSDateFormatter for a printable date. When you NSLog an NSDate directly you get GMT, by design.
How can I get the month, day, year, day of week, time, into separate values from a NSString like this "2013-09-27 15:05:00"?
For example 2013-09-27 15:05:00
NSString month: September
NSString year: 2013
NSString day: 27
NSString weekday: Monday
NSString time: 15:05:00
Please help me!
You may do it using NSDateComponents as #Mikael said, or the other way is:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *myDate = [df dateFromString: #"2013-09-27 15:05:00"];
[df setDateFormat:#"dd"];
myDayString = [df stringFromDate:myDate];
[df setDateFormat:#"MMM"];
myMonthString = [df stringFromDate:myDate];
[df setDateFormat:#"yy"];
myYearString = [df stringFromDate:myDate];
[df setDateFormat:#"HH:mm:ss"];
myTimeString = [df stringFromDate:myDate];
[df release];
First you need a NSDate from your NSString, then you can separate the values with NSDateComponents
This should do it:
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *myDate = [df dateFromString: #"2013-09-27 15:05:00"];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:myDate];
Use dateFromString: method of NSDateFormatter class. You will get NSDate object back. Once you have it, you can do further with it whatever you want. This is basically a date and time parser with bunch of options for every situation. Writing your own parser would be a bad idea...
Read completely Date and Time Programming Guide in Apple Developer Documentation. Date and time are not trivial things, it is wise to be well informed how you can hand this information in different situations, especially when writing international apps.
Untested code but it should work..please try
NSCalendar *myCalendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *myDate = [dateFormatter dateFromString: #"2013-09-27 15:05:00"];
NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:myDate];
NSString *day = [NSString stringWithFormat=#"%d", [dateComponents day]];
NSString *month = [NSString stringWithFormat=#"%d", [dateComponents month]];
NSString *year = [NSString stringWithFormat=#"%d", [dateComponents year]];