iPhone - Conversion between NSDate - NSString - NSDate - objective-c

I'm using following code to get NSDate, convert it to NSString and then back t NSDate.
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:#"MM/dd/YYYY"];
NSDate* dt = [NSDate date];
NSString* strDate = [dtFormatter stringFromDate:dt];
NSLog(#"First = %#",strDate);
NSDate* dt2 = [dtFormatter dateFromString:strDate];
NSString* strDate2 = [dtFormatter stringFromDate:dt2];
NSLog(#"Second = %#",strDate2);
But the output I'm getting is different:
2011-02-12 08:17:21.851 SF Calculation[16297:207] First = 02/12/2011
2011-02-12 08:17:21.852 SF Calculation[16297:207] Second = 12/26/2010
Why is it different when converting NSDate to NSString and back from NSString to NSDate?

Instead of :
[dtFormatter setDateFormat:#"MM/dd/YYYY"];
use:
[dtFormatter setDateFormat:#"MM/dd/yyyy"];

Related

UIDatePicker Order Format

I am new to ios development tried UIDatepicker format like day month date. I tried alot but not able to get, Can any one help ?
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewController.selectedDate"];
storedDate = [dateFormat dateFromString:#"EE,MMMM dd"];
// add this check and set
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.pickerView setDate:storedDate animated:NO];
i need the above image format
First, get the saved date in string Format.
NSString *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewControllerselectedDate"];
Then get the format in which the date is stored so that you can convert it to date.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
Now you can convert the stored-date to date format and set in datepicker.
NSDate *sortedDateFormatted = [format dateFromString:storedDate];
[self.pickerView setDate:sortedDateFormatted animated:NO];
You need the date format ryt, Just try this
NSDate *now = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
NSString * dateStr = [[format stringFromDate:now] capitalizedString];
NSDate *sortedDateFormatted = [format dateFromString:dateStr];
[self.pickerView setDate:sortedDateFormatted animated:NO];

Get value from another method

I am trying to use data that I have placed on the same class but in a different method (I believe that is the terminology) but I cannot seem to access this data for some odd reason. I can do so when I place it all together (in the timer) but that would consume a TONNE of memory would it not since it would be setting this data over and over. Anyway, this is my code so far:
/////////////////////////////////SET TIME/DAYS///////////////////
currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.timeNow.text = [timeFormatter stringFromDate:currentTime];
NSDate *lesson1Start = [timeFormatter dateFromString:#"8:40"];
NSDate *lesson2Start = [timeFormatter dateFromString:#"9:35"];
NSDate *recessStart = [timeFormatter dateFromString:#"10:30"];
NSDate *caregroupStart = [timeFormatter dateFromString:#"10:50"];
NSDate *lesson3Start = [timeFormatter dateFromString:#"11:00"];
NSDate *lesson4Start = [timeFormatter dateFromString:#"11:55"];
NSDate *lunchStart = [timeFormatter dateFromString:#"12:50"];
NSDate *lesson5Start = [timeFormatter dateFromString:#"1:30"];
NSDate *lesson6Start = [timeFormatter dateFromString:#"2:20"];
NSDate *endDay = [timeFormatter dateFromString:#"3:10"];
}
//////////////////////////END SET TIMES/////////////////////////
- (void)updateTime {
NSString *mondayDay = (#"Monday");
NSString *tuesdayDay = (#"Tuesday");
NSString *wednesdayDay = (#"Wednesday");
NSString *thursdayDay = (#"Thursday");
NSString *fridayDay = (#"Friday");
[updateTimer invalidate];
updateTimer = nil;
currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.timeNow.text = [timeFormatter stringFromDate:currentTime];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:#selector(updateTime)
userInfo:Nil repeats:YES];
///////Check day/time & update lesson/////
if (self.dayNow.text == mondayDay && self.timeNow.text == lesson1Start) {
NSLog(#"");
}
As you can see, I cannot use "lesson1Start" since it is not in - (void)updateTime.
Would it be fine if I did bundle it all noting that it would be setting this data over and over since it is placed in the timer.
PS: I am trying to check if when the lesson starts and if the time is when the lesson starts then the lesson would appear. In this case I just have NSLog(#"").
You can do as...
Lets consider your code is in class ClassRoom;
#interface ClassRoom
{
NSArray *scheduleArray; //instance variable
}
you can create a NSArray *scheduleArray; for holding //you have to allocate and init it in implementation file
NSDate *lesson1Start = [timeFormatter dateFromString:#"8:40"];
NSDate *lesson2Start = [timeFormatter dateFromString:#"9:35"];
NSDate *recessStart = [timeFormatter dateFromString:#"10:30"];
NSDate *caregroupStart = [timeFormatter dateFromString:#"10:50"];
NSDate *lesson3Start = [timeFormatter dateFromString:#"11:00"];
NSDate *lesson4Start = [timeFormatter dateFromString:#"11:55"];
NSDate *lunchStart = [timeFormatter dateFromString:#"12:50"];
NSDate *lesson5Start = [timeFormatter dateFromString:#"1:30"];
NSDate *lesson6Start = [timeFormatter dateFromString:#"2:20"];
NSDate *endDay = [timeFormatter dateFromString:#"3:10"];
add all above dates in scheduleArray.(Hope you know how to add items in NSArray.)
Now you can write updateTime as
- (void)updateTime {
NSString *mondayDay = (#"Monday");
NSString *tuesdayDay = (#"Tuesday");
NSString *wednesdayDay = (#"Wednesday");
NSString *thursdayDay = (#"Thursday");
NSString *fridayDay = (#"Friday");
[updateTimer invalidate];
updateTimer = nil;
currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.timeNow.text = [timeFormatter stringFromDate:currentTime];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:#selector(updateTime)
userInfo:Nil repeats:YES];
///////Check day/time & update lesson/////
for(NSDate * date in scheduleArray){
here you will get above saved dates use them to your need.
}
}
Hope it helps !

NSString to NSDate is NOT working when called multiple times

I have written this function in a class:
- (NSDate *) convertDate : (NSString *) dateStr{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateFrmStr = [[NSDate alloc] init];
dateFrmStr = [dateFormatter dateFromString:dateStr];
return dateFrmStr;
}
I am calling this function in another class like this:
NSString * dateStr1 =#"01-01-1977";
NSString * dateStr2 =#"22-12-1977";
NSString * dateStr3 =#"19-01-1978";
MyClass *data = [[MyClass alloc]init];
NSDate *dateObj1 = [data convertDate:dateStr1];
NSDate *dateObj2 = [data convertDate:dateStr2];
NSDate *dateObj3 = [data convertDate:dateStr3];
NSLog(#" >>> dateObj1 %#",dateObj1);
NSLog(#" >>> dateObj2 %#",dateObj2);
NSLog(#" >>> dateObj3 %#",dateObj3);
When I run this the only first date seems to get converted because the output I get is :
>>> dateObj1 1977-01-01 00:00:00 +0000
There is no error nothing but the programs just stops.
I've tried you're code and for me works very fine( with ARC ), probably you're issue is somewhere else in "MyClass"…
BTW! Why don't you make a "category" on NSDate?
Something like this :
NSDate + ConvertDate.h
#interface NSDate (ConvertDate)
+(NSDate *) convertDateFromString : (NSString *)dateString;
#end
NSDate + ConvertDate.m
#implementation NSDate (ConvertDate)
+(NSDate *)convertDateFromString:(NSString *)dateString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
return [dateFormatter dateFromString:dateString];
}
#end
Yes it looks like your code, the only differences are that you don't need to allocate an instance of "MyClass" to use the
"convertDateFromString" method because you're using a class method.'
could it be
NSString * dateStr2 =#"22-12-1977";
that NSDate thinks 22 as the month??? maybe try 12-22-1977 instead?? i usually use NSDateFormatter to convert to string or date..

date from string not working

CODE:
NSString* mydate = #"11/06/2012 19:15:40";
NSLog(#"%#", mydate);
NSDateFormatter* dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM-dd-yyyy HH:mm:ss";
NSDate* adate = [dateFormatter1 dateFromString:mydate];
NSLog(#"%#", adate);
LOG:
11/06/2012 19:15:40
(null)
Why am I getting null?
Your date string is using slashes but your format specifier is setup to look for dashes.

Localized date (month and day) and time with NSDate

I want to be able to get the local date and time for wherever my app is run, based on the iPhone configuration. Specifically I need the date to be of format mm/dd or dd/mm (or dd.mm, mm.dd, dd-mm, mm-dd, etc) depending on the locale, and time is hh:mm. Is this possible with some combination of SDK methods?
Thanks!
I have modified the code so that it just takes the date and time out of the NSDate object with no changes:
NSDate* date = [NSDate date];
NSString* datePart = [NSDateFormatter localizedStringFromDate: date
dateStyle: NSDateFormatterShortStyle
timeStyle: NSDateFormatterNoStyle];
NSString* timePart = [NSDateFormatter localizedStringFromDate: date
dateStyle: NSDateFormatterNoStyle
timeStyle: NSDateFormatterShortStyle];
NSLog(#"Month Day: %#", datePart);
NSLog(#"Hours Min: %#", timePart);
Well, I believe the following code works for what I need:
NSString *dateComponents = #"yMMd";
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale currentLocale]];
NSArray *tmpSubstrings = [dateFormat componentsSeparatedByString:#"y"];
NSString *tmpStr;
NSRange r;
if ([[tmpSubstrings objectAtIndex:0] length] == 0) {
r.location = 1;
r.length = [[tmpSubstrings objectAtIndex:1] length] - 1;
tmpStr = [[tmpSubstrings objectAtIndex:1] substringWithRange:r];
} else {
r.location = 0;
r.length = [[tmpSubstrings objectAtIndex:0] length] - 1;
tmpStr = [[tmpSubstrings objectAtIndex:0] substringWithRange:r];
}
NSString *newStr = [[NSString alloc] initWithFormat:#"%# H:mm", tmpStr];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:newStr];
NSString *formattedDateString = [formatter stringFromDate:[NSDate date]];