how to create the nscalender and gregorian calender - iphone

how to create the nscalender and gregorian calendar?
thank you

You can initialize and use gregorian calendar like:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1965];
[comps setMonth:1];
[comps setDay:6];
[comps setHour:14];
[comps setMinute:10];
[comps setSecond:0];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorianCalendar dateFromComponents:comps];
and the current calendar like:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1965];
[comps setMonth:1];
[comps setDay:6];
[comps setHour:14];
[comps setMinute:10];
[comps setSecond:0];
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];

Related

Getting next date and previous day date

I basically want to convert a particular date (x) to the previous day date(x - 1 day) and also to the next day date (x + 1 day). I am using the following code for this :
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
However I have my date (x) in NSString format, and I need to convert the NSString(myDateString) to NSDate(myDate) before applying the above code.
I have a NSString containing date in format MM-dd-yyyy.
For converting I am using the following code , but I am getting absurd values.
NSLog(#"myDateString=%#",myDateString);//output:myDateString=10-25-2012//all correct
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM-dd-yyyy"];
NSDate *myDate=[formatter dateFromString:myDateString];
NSLog(#"myDate=%#",myDate);//output:myDate=2012-10-24 18:30:00 +0000 // I only want the date to be shown // and why has the format changed
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
NSLog(#"datePlusOneDay=%#",datePlusOneDay);//output:datePlusOneDay=2012-10-25 18:30:00 +0000// I only want the date to come , not time // and why has the format changed
Later again I need to convert the NSDate to NSString
NSString *curentString=[formatter stringFromDate:datePlusOneDay];
NSLog(#"curentString=%#",curentString); //output:curentString=10-26-2012
Similarly I also want to get the previous date.
Please help guys !! and ho ho MERRY CHRISTMAS !!
Do as: componets.day = 1 to obtain the next, -1 for the previous day.
NSDate *date = [NSDate date]; // your date from the server will go here.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];
NSLog(#"newDate -> %#",newDate);
The below code should get you the previous date:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:-1]; // replace "-1" with "1" to get the datePlusOneDay
NSDate *dateMinusOneDay = [gregorian dateByAddingComponents:offsetComponents toDate:myDate options:0];
Merry Xmas :)
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
Try this simple solution for previous date:-
NSDate *datePlusOneDay = [[NSDate date] dateByAddingTimeInterval:-(60 * 60 * 24)];
NSLog(#"datePlusOneDay=%#",datePlusOneDay);
Swift 5 Solution
let calendar = Calendar.current
calendar.date(byAdding: .day, value: -2, to: Date())
function for previousDay
-(void)previousDay
{
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
dateString = [dateFormat stringFromDate:today];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day =-1;
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
EndingDate = [dateFormat stringFromDate:sevenDays];
}
function for next day
-(void)nextDay
{
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
dateString = [dateFormat stringFromDate:today];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day =1;
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
EndingDate = [dateFormat stringFromDate:sevenDays];
}

How to get week startdate and week enddate from iphone calendar

I am new to iphone.I am doing one project in that project i am struck in the concept of getting week startdate and week enddate of iphone calendar from my application.If anybody know this concept please help me...
Here, I am position code. See it that will help you.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];// you can use your format.
NSString *dateString = [dateFormat stringFromDate:today];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
int dayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today] weekday];// this will give you current day of week
[components setDay:([components day]-(dayofweek))];// for beginning of the week.
// or
[components setDay:([components day]+(7-dayofweek))];// for end day of the week
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:#"yyyy-MM-dd"];
NSString *dateString2 = [dateFormat stringFromDate:beginningOfWeek];
Hope, this will help you...
This will help you
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];
[components setDay:([components day] - ([components weekday] - 1))];
NSDate *thisWeek = [cal dateFromComponents:components];
[components setDay:([components day] - 7)];
NSDate *lastWeek = [cal dateFromComponents:components];
[components setDay:([components day] - ([components day] -1))];
NSDate *thisMonth = [cal dateFromComponents:components];
[components setMonth:([components month] - 1)];
NSDate *lastMonth = [cal dateFromComponents:components];
NSLog(#"today=%#",today);
NSLog(#"yesterday=%#",yesterday);
NSLog(#"thisWeek=%#",thisWeek);
NSLog(#"lastWeek=%#",lastWeek);
NSLog(#"thisMonth=%#",thisMonth);
NSLog(#"lastMonth=%#",lastMonth);
from Here

How to create a custom date on iPhone

I'm trying to set some components of todays date with NSDateComponent like so:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setHour:1];
[comps setMinute:44];
NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date = [cal dateByAddingComponents:comps toDate:[NSDate date] options:0];
[comps release];
NSLog(#"%#", date);
But this example will ADD the time to the current date. Now what I want to do is ADD one day but set the hour and minute to the specified values (no adding). How can I do this?
This worked in the end:
NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSYearCalendarUnit
NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[comp setDay:[comp day] +1];
[comp setHour: 12];
[comp setMinute: 00];
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comp];
Easy: Just use the date property...
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setHour:1];
[comps setMinute:44];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
Edit: forgot the calendar. From the manual (1st page of NSDateComponents in bold):
Important: An NSDateComponents object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units.
There's also an example of how to use it.

NSDate Question

How do I create a NSDate of August 1, 2005? Found NSDateComponent on the internet, but seems too complicated.
Another possibility is to use NSDateComponents and NSCalendar. For instance:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2000];
[comps setMonth:1];
[comps setDay:1];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *startDate = [cal dateFromComponents:comps];
[cal release];
[comps release];
What about:
[NSDate dateWithNaturalLanguageString:#"August 1st, 2005"];
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"M/d/yy"];
NSDate *myDate = [myFormatter dateFromString:#"August/01/05"];
It rather depends on your timezone, but in GMT, the 1st of August 2005 at 1 minute past midnight was the UNIX timestamp 1122854401. So you can write...
[NSDate dateWithTimeIntervalSince1970:1122854401];
Introduction to Date and Time Programming Guide for Cocoa
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
[components setMonth:8];
[components setYear:2005];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];

Taking the current Time and subtracting 3 hours from it

was wondering on how to take the current Time, and subtracting 3 hours from it to be stored in NSString?
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:-3];
NSDate *threeHoursAgo = [calendar dateByAddingComponents:comps toDate:now options:0];
[comps release];
[calendar release];
I leave the output as an NSString to you (use NSDateFormatter).
How about the [[NSDate alloc] initWithTimeIntervalSinceNow: -3*60*60] and using an NSDateFormatter?