Why does this NSDate gotten from NSString have the month switched? - ios

Im sending a string to a dateFormatter with this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CST"]];
NSDate *currentDate = [NSDate date];
NSCalendar* calendario = [NSCalendar currentCalendar];
NSDateComponents* components = [calendario components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
//make new strings
NSString *adjustedOpenDateString = [NSString stringWithFormat:#"%#-%ld-%ld %#", #"2013", (long)[components month], (long)[components day], openDateString];
NSString *adjustedCloseDateString = [NSString stringWithFormat:#"%#-%ld-%ld %#", #"2013", (long)[components month], (long)[components day],closeDateString];
NSLog(#"adjustedString %#", adjustedCloseDateString); //<<<<---NSLOG1
//Convert strings to dates
NSDate *openDate = [dateFormatter dateFromString:adjustedOpenDateString];
NSDate *closeDate = [dateFormatter dateFromString:adjustedCloseDateString];
NSLog(#"BEFORE COMPONENTS open, now, close %#,%#,%#", openDate,[NSDate date], closeDate); //<<<<---NSLOG2
if ([self timeCompare:openDate until:closeDate]) {
NSLog(#"OPEN-timeCompare");
} else {
NSLog(#"CLOSED-timeCompare");
}
And I get the adjustedString NSLog logging the current date correctly:
adjustedString 2013-7-25 10:00 PM
But when I log the dates converted from strings, the month is lost...switched from july to jan
open, now, close 2013-01-25 13:00:00 +0000,2013-07-26 02:54:31 +0000,2013-01-26 04:00:00 +0000
Why does this happen?

Your format string is wrong, for month you have to use "MM", so replace:
[dateFormatter setDateFormat:#"yyyy-mm-dd hh:mm a"];
with
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
Take a look at Date Format Patterns, Unicode Technical Standard #35.

Switch your format string to #"yyyy-MM-dd hh:mm a" According to the Unicode Technical Standard #35, which NSDateFormatter uses for date format strings, lowercase 'm' is for minute and uppercase is for month.

Related

i need to convert time to date format

time is string format like 10:27:43. but when i convert in to date format. it is going to change Sat Jan 1 10:27:43 2000.but i need only time like 10:27:43
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSLog(#"Date is %#", dateTime);
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:mm:ss"];
newDate = [dateFormatter stringFromDate:dateTime];
NSLog(#" string Date is %#", newDate);
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
//[dateFormatter1 setDateFormat:#"dd/mm/yyyy"];
NSDate *dateFromString = [dateFormatter1 dateFromString:newDate];
NSLog(#"date format date %#",dateFromString);
you need to convert dateFormat as per date is coming and after convert it as you want.
Try this below code..
NSString *dateTime = #"Thu Jan 25 10:38:11 2018";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:dateTime];
NSLog(#"Date is %#", dateTime);
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *result = [dateFormatter stringFromDate:dateFromString];
NSLog(#"result %#",result);
Split the time in date components and use that components as a time.
Check below code.
NSString *aStrTime = #"10:27:43";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:aStrTime];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger sec = [components second];
NSLog(#"%d:%d:%d", hour, minute, sec);
Note: change aStrTime and dateFormatPattern according to your need.
You can try the following code:-
For e.g:- if you are getting the date in 10-02-2017 12:30:50 format
NSString *strDate = #"10-02-2017 12:30:50";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-mm-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:strDate];
NSLog(#"The Date is: %#", date);
// By this code you will get the date in NSDate form. Now you can change the format to get the time
[dateFormat setDateFormat:#"HH:mm:ss"];
NSString *strFinalTime = [dateFormatter stringFromDate:strDate];
NSLog(#"strFinalTime: %#",strFinalTime);

How to format the string date and time to Tuesday 4 December 2012,5:30 PM

I have a small doubt regarding the conversion of a date and time string to (Tuesday 4 December 2012,5:30 PM) in Objective c
NSString *str = [detailsdict objectForKey:#"doc_appointments_Time"];
//time format is 5:30:00 PM
NSString *dateandtime=[NSString stringWithFormat:#"%#, %#",[detailsdict objectForKey:#"doc_appointments_Date"],str];
//date format is 10/25/2016
cell.detailTextLabel.text=dateandtime;
For that first you need to merge both the date and time string after that use NSDateFormatter to generate date in a format you want.
NSString *stringDate = [NSString stringWithFormat:#"%# %#",[detailsdict objectForKey:#"doc_appointments_Date"], [detailsdict objectForKey:#"doc_appointments_Time"]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [formatter dateFromString:stringDate];
[formatter setDateFormat:#"EEEE d MMMM yyyy,h:mm a"];
NSString *formattedDate = [formatter stringFromDate:date];

How can I determine next closest day based on the weekday in objective c?

How come the following still displays in GMT? I am trying to get it to display in EST Toronto so I Set the NSTimeZone to localTimeZone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-dd-MM"];
NSString *todayDateString = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]
sinceDate:[NSDate date]]];
[dateFormatter setDateFormat:#"yyyy-dd-MM hh:mm:ss +0000"];
NSDate *bus500_monday_To_Friday_T1 = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# 06:40:00 +0000", todayDateString]]; // 6:40 AM
NSDate *bus500_monday_To_Friday_T2 = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# 07:50:00 +0000", todayDateString]]; // 7:50 AM
NSLog(#"%#", bus500_monday_To_Friday_T2);
If I understand correctly that you need today's date in your strings other than manually changing hardcoded strings, then you should use this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"yyyy-dd-MM"];
NSString *todayDateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter setDateFormat:#"yyyy-dd-MM hh:mm:ss +0000"];
NSDate *bus500_monday_To_Friday_T1 = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# 06:40:00 +0000", todayDateString]]; // 6:40 AM
NSDate *bus500_monday_To_Friday_T2 = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# 07:50:00 +0000", todayDateString]]; // 7:50 AM
//use above code for all other date variables too.
// rest code would be same
After this, you can use date comparisons methods to know which next bus time is nearest to current device time.

NSDate Showing Wrong Day

I am taking an NSDate, and pulling just a 2-digit number, representing the day of the month into an NSString. One of the dates in question is:
2013-11-30 00:00:00 +0000
I use:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"dd"];
NSString *datefromdate = [formatter2 stringFromDate:articleDate];
NSLog(#"Date%#", datefromdate);
[formatter2 release];
but the log comes back
29
You are probably in a negative time zone i.e. GMT minus something. This is why 2013-11-30 00:00:00 +0000 GMT is on the 29th day when you log it. Set the formatter to GMT and you will be fine.
Set the timezone you want the time date formatter to use. NSDate is the first instant of 1 January 2001, GMT and thus has no timezone information in it.
So, this, according to Apple, is going to get complicated needing up to five classes: NSDateFormatter, NSDate, NSCalendar, NSTimeZone and finally NSDateComponents.
If all you want is the day you can use NSDateComponents.
Example:
NSString *dateString = #"2013-11-30 00:00:00 +0000";
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
[inDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [inDateFormatter dateFromString:dateString];
NSLog(#"dateFromString: %#", date);
NSTimeZone *timezone = [NSTimeZone timeZoneForSecondsFromGMT:0];
// Using date formatter, result is a string
NSDateFormatter *outDateFormatter = [NSDateFormatter new];
[outDateFormatter setTimeZone:timezone];
[outDateFormatter setDateFormat:#"dd"];
NSString *dayString = [outDateFormatter stringFromDate:date];
NSLog(#"date formatter day: %#", dayString);
// Using date components, result is an integer
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone: timezone];
NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit fromDate:date];
NSInteger day = [dateComponents day];
NSLog(#"date components day: %i", day);
NSLog output:
dateFromString: 2013-11-30 00:00:00 +0000
date formatter day: 30
date components day: 30

NSDateFormatter wrong string after formatting

I receive a date through a string parameter, which is tempDateString, in a [day month year] format (for ex. 01 05 2005):
NSLog(#"tempdatestring %#", tempDateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM YYYY"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(#"daydate %#", dayDate);
My problem is, that the two logs don't match. The outputs are:
tempdatestring 04 10 2012
daydate 2011-12-24 22:00:00 +0000
What should I change at the date formatter's date format, to get the good date?
2 Problems
your format is wrong it is #"dd MM yyyy" case sensitive
Use timezone to get the correct value[GMT value]
NSString *tempDateString=#"04 10 2012" ;
NSLog(#"tempdatestring %#", tempDateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"dd MM yyyy"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(#"daydate %#", dayDate);
Do this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(#"daydate %#", dayDate);
NSString *strDate = [dateFormatter stringFromDate:dayDate];
NSLog(#"strDate :%#",strDate);
When you use the %# format specifier, the return value of the -description method invoked on the provided object is used.
NSDate's -description method outputs its value in that specific way.
Your real problem though is that your date format string is incorrect - it should be dd MM yyyy.
I stuck this in a sample Xcode project:
NSString *s = #"04 11 2012";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd MM yyyy"];
NSDate *d = [df dateFromString:s];
NSDateComponents *c = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:d];
NSLog(#"%#", c);
It gave me the following output:
2012-10-04 01:53:24.320 dftest[59564:303] <NSDateComponents: 0x100113e70>
Calendar Year: 2012
Month: 11
Leap month: no
Day: 4
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:#"dd MM yyyy"];
form.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:-7200.0];
NSDate *dayDate = [form dateFromString:#"05 10 2012"];
NSLog(#"daydate %#", dayDate);
NSString *strDate = [form stringFromDate:dayDate];
NSLog(#"strDate %#",strDate);
Change date format to #"dd MM yyyy". After this, dateFromString may still parse the wrong date (in my case it was yesterday 21-00). To avoid this I've set TimeZone in my DateFormatter:
form.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:-7200.0];
-7200.0 is my timezone, you should change this to yours ("0" sets to Greenwich). After this log looks like:
daydate 2012-10-05 02:00:00 +0000
strDate 05 10 2012

Resources