Can u please tell me how to extract a date from UIDatePicker, i have extracted the month but i am not able to extract the date,Please help me out
Thank You
The NSDate object can be extracted by just accessing the date property of the object:
NSDate *dateFromPicker = [someDatePicker date];
If you want to extract detailed information about what month, day and year a person has chosen, this is a little bit more complex (but just a little bit). The solution is to use an NSCalendar reference - it is likely that you'd want to use Gregorian.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
Now you should use an unsigned integer unit flags to specify which units you want to extract from a date (i.e. day, day-of-the-month, year, era etc.). Use a bitwise | to select multiple unit flags:
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit;
Now create a date components object:
NSDateComponents *components = [calendar components: unitFlags fromDate: dateFromPicker];
From this components date, you can now extract integer values of what you have mentioned in the unitFlags integer:
int year = [components year];
int month = [components month];
All other properties (i.e. day, day-of-the-month, era etc.) are undefined in such a components object - remember, you can only extract what you've explicitly said you want to extract.
Finally, free the calendar object to avoid memory leaks:
[calendar release];
Hope this helps.
For more information, see Apple reference:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html
You can access the date property of the UIDatePicker class to get the date!
Related
I am using core data in my project. Database is sqlite and there is column for storing "birthdate" having datatype "timestamp".
Now, I want to insert some records in it and I am having dates in human readable format like "1809-06-17".
How to convert this date to timestamp so that, I get this date when fetched from database.
I tried some conversions using python script, but I got different results.
Is there any easy way to do this? Please help me.
You could store the date as TEXT and dont have to worry about converting to and from timestamp.
To convert timestamp to NSDate use:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
NSLog(#"%#", date);
String to NSDate to timestamp:
NSDate *date = [NSDate dateWithString:stringWithDate];
NSTimeInterval timeStamp = [date timeIntervalSince1970];
SQLite doesnt have a datatype for Date, it just stores it in a INT (Unix Time), REAL(Julian day numbers) or TEXT(ISO8601 strings) depending on what you choose.
So you could just store the date in the format you already have ("1809-06-17") as a TEXT column or you store as a timestamp in an INT column using the methods above to convert both ways.
SQLite Date and Time functions: SQLite date and time
SQLite DATATYPES: SQLite datatypes doc
SQLite Oficial Site and Documentation: SQLite Home
Hope you can solve your problem
Finally I got the solution I needed.
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate * past = [df dateFromString:#"1971-04-06"];
NSTimeInterval oldTime = [past timeIntervalSinceDate:[df dateFromString:#"2001-01-01"]];
NSString * unixTime = [[NSString alloc] initWithFormat:#"%0.0f", oldTime];
NSLog(#"unixTime = %#",unixTime);
I am doing a birthday remainder app. In that i need to show the no of days left for birthday.
If a user Chooses a particular birthday date in the date picker i need to update automatically how many days left for his birthday from the date he chosen. Can any one help this through coding.
Thanks!
we can do it using nsdate using we can get the present date and then we have to calculate how many days are there between today date and selected date
Below code is use for the findout the interval between the to date and time for that below code is help for that
NSDateComponents *comp=[[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date] toDate:"Your date is here" options:0] ;
cell.detailTextLabel.text=[NSString stringWithFormat:#"%d : %d : %d : %d : %d : %d",comp.year,comp.month,comp.day,comp.hour,comp.minute,comp.second];
It may to help for your application
Happy coding
#samuel
I have a NSDate variable "aDate" that represents a date, for example, sunday, August 28, 2011. I have this date on a NSDate variable. This can be any date, but the problem appears when the day is a sunday.
I want to obtain the three letter string representing the day of week. In this case, SUN.
Then I have this code:
NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEE"];
[theDateFormatter setLocale:currentLocale]; // I have tried to comment this out... no change
NSString *dayOfWeek = [theDateFormatter stringFromDate:aDate];
the result I have is MON. Monday?
The insanity just goes wilder if I add this code:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:myDate];
int weekday = [comps weekday];
then weekday comes as 2 (MONDAY ??????) WTF? Confirming the problem.
I am on a locale that in theory monday is the first day of week. So, independently of my timezone any date just have one day of week. If I want to calculate what day of week was January 1, 1978, I shouldn't need to define any timezone.
How do I do that on iPhone?
thanks.
NSData is referenced to a base date "the first instant of 1 January 2001, GMT", it is simply the number of seconds since that base date.
Dates by definition are location sensitive, as I write this my friend in the UK is already in tomorrow.
Day of week is calendar sensitive.
Thus the timezone and calendar need to be specified. In my case they seem to be correct by default but are best specified explicitly.
So, set the timezone:
- (void)setTimeZone:(NSTimeZone *)tz
What's your time zone? Chances are you are one day off because your time zone is sufficiently different from GST that the time you are representing is the next or previous day.
I have found it necessary to specify the date as being in the local time zone to avoid this.
Using the Gregorian calendar, I am able to get the number of the day of the week (i.e. 1 for Sunday and 2 for Monday, etc.) but I am not able to find a function that displays the name of week then the number. Can anyone help?
Here is my code to get the number of day:
NSDate *dates = [gregorian dateFromComponents:component];
NSDateComponents *weekdayComponents =[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:dates];
NSUInteger *weekdays = [weekdayComponents weekday];
NSString *dayw=[NSString stringWithFormat:#"%1i",weekdays];
NSLog(#"%#",dayw);
Create an NSDateFormatter. The format string #"EEEE" will output the name of the day of the week.
I'm looking two do two things:
Check if a week has gone by (based on the day of the week)
Check if a day has gone by (based on the date)
I'm not sure what the best way to solve this is, since when comparing two NSDates (one at 1/1/09 at 21:30 and another at 1/2/09 at 00:30) I can't just see if 24 hours has gone by since in this case it hasn't. I can solve this by just getting the date 1/1/09 from the NSDate, but I'm unsure how to do this based on the documentation and google links that I saw.
For the other issue, I don't know how to know what day of the week it is given a date. I'd like to if 1/1/09 is a Tuesday, Monday, etc... Is there any library that let's you calculate this? This obviously needs to take into account leap years and a tons of other stuff... Maybe there's an objective-c library for this?
Thanks for any help!
You can use components:fromData: message of the NSCalendar class to get the individual date components:
// Sunday = 1, Saturday = 7
int weekday = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:theDate] weekday];
To compare two dates, just use the compare: message:
switch([date1 compare:date2])
{
case NSOrderedSame: /* they are the same date */ break;
case NSOrderedAscending: /* date1 precedes date2 */ break;
case NSOrderedDescending: /* date2 precedes date1 */ break;
}
You can use NSDateComponents for this:
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:units fromDate:date];
then you can access individual parts of the date like this:
[components year];
[components month];
[components day];
Or, you can construct a new NSDate object using NSCalendar's dateFromComponents method, and compare the two NSDate objects.