how to get a particular day of week in objective c? - iphone

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.

Related

IsoWeekYear within DAX

Does anyway have a way of creating IsoWeekYear within DAX?
I use the following within excel to create, however am struggling to get this working within PowerBI
=YEAR(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,1)-MOD((DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,1)-2),7)+(7*IF(MOD((DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,1)-2),7)>3,1,0))+7)
You'll need to use the DAX WEEKNUM function. It has an optional parameter for the ISO 8601 week. Option 21 will return the ISO Week
ISO Week Number = WEEKNUM([DateColumn], 21)
To get the ISO Year, you'll need a calendar week and year column as well.
Calendar Week = WEEKNUM([DateColumn])
Year = YEAR([DateColumn])
Year Week = IF([ISO Week Number]<5 && [Calendar Week] > 50,
[Year]+1,
IF([ISO Week Number] >50 && [Calendar Week]<5,
[Year]-1,
[Year]))
You can do this all in one column, rather that split them out in this example and concat, year and week number to be 202101, or keep them separate, to whatever type you need.

Finding difference between the dates from recent date to same date on next year

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

iPhone - Calculating the Day of Week of any date

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.

Extract date from UIDatePicker

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!

How to check what day of the week it is (i.e. Tues, Fri?) and compare two NSDates?

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.