怎么创建一个nscalendar

作者&投稿:融阅 (若有异议请与网页底部的电邮联系)
怎么创建一个nscalendar~

使用currentCalendar函数就可以了。例如:
NSCalendar * myCal = [NSCalendar current Calendar];


下面是苹果官网的说明:

System Locale Information







+ currentCalendar







Returns the logical calendar for the current user.



Declaration


Swift

class func currentCalendar() -> NSCalendar




Objective-C

+ (NSCalendar *)currentCalendar





Return Value

The logical calendar for the current user.



Discussion

The returned calendar is formed from the settings for the current
user’s chosen system locale overlaid with any custom settings the user
has specified in System Preferences. Settings you get from this calendar
do not change as System Preferences are changed, so that your
operations are consistent (contrast with autoupdatingCurrentCalendar).


参考:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/occ/clm/NSCalendar/currentCalendar

calendars
n.历法; 日程表; 日历( calendar的名词复数 ); (一年之中的) 重大事件(或重要日期)一览表;
[例句]Wizards and templates can help you create brochures, calendars, and Web pages.
向导程序和模板可以帮助你创建手册、日历和网页。

nsc alendar unit
NSC电子日历单元

参考:http://www.iliunian.com/1105.html
一、NSDate
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间, NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能;Date对象是不可改变的。
如果你要创建date对象并表示当前日期,你可以alloc一个NSDate对象并调用init初始化。
NSDate *now = [[NSDate alloc] init];
或者使用NSDate的date类方法来创建一个日期对象。如果你需要与当前日期不同的日期,你可以使用NSDate的initWithTimeInterval或dateWithTimeInterval方法,你也可以使用更复杂的calendar或date components对象。

1)创建一定时间间隔的NSDate对象:
[objc] view plaincopy
NSTimeInterval secondsPerDay = 224 * 660 * 60;
NSDate *tomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay];
NSDate *yesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];
[tomorrow release];
[yesterday release];
如果要对NSDate对象进行比较,可以使用isEqualToDate:, compare:, laterDate:和 earlierDate:方法。这些方法都进行精确比较,也就是说这些方法会一直精确比较到NSDate对象中秒一级。例如,你可能比较两个日期,如果他们之间的间隔在一分钟之内则认为这两个日期是相等的。在这种情况下使用,timeIntervalSinceDate:方法来对两个日期进行比较。下面的代码进行了示例:
[objc] view plaincopy
if fabs(([date2 timeIntervalSinceDate:date1]) < 60)

二、NSCalendar & NSDateComponents
日历对象封装了对系统日期的计算,包括这一年开始,总天数以及划分。你将使用日历对象对绝对日期与date components(包括年,月,日,时,分,秒)进行转换。
NSCalendar定义了不同的日历,包括佛教历,格里高利历等(这些都与系统提供的本地化设置相关)。NSCalendar与NSDateComponents对象紧密相关。你可以通过NSCalendar对象的currentCalendar方法来获得当前系统用户设置的日历。
[objc] view plaincopy
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSCalendar *japaneseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSJapaneseCalendar];
NSCalendar *usersCalendar = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar];
usersCalendar和currentCalendar对象是相等的,尽管他们是不同的对象。
你可以使用NSDateComponents对象来表示一个日期对象的组件——例如年,月,日和小时。如果要使一个NSDateComponents对象有意义,你必须将其与一个日历对象相关联。
[objc] view plaincopy
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:6];
[components setMonth:5];
[components setYear:2004];
NSInteger weekday = [components weekday]; // Undefined (== NSUndefinedDateComponent)
要将一个日期对象解析到相应的date components,你可以使用NSCalendar的components:fromDate:方法。此外日期本身,你需要指定NSDateComponents对象返回组件。
[objc] view plaincopy
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];

NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];
同样你也可以从NSDateComponents对象来创建NSDate对象:
[objc] view plaincopy
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:2]; // Monday
[components setWeekdayOrdinal:1]; // The first Monday in the month
[components setMonth:5]; // May
[components setYear:2008];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
为了保证正确的行为,您必须确保使用的组件在日历上是有意义的。指定“出界”日历组件,如一个-6或2月30日在公历中的日期值产生未定义的行为。
你也可以创建一个不带年份的NSDate对象,这样的操作系统会自动生成一个年份,但在后面的代码中不会使用其自动生成的年份。
[objc] view plaincopy
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMonth:11];
[components setDay:7];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *birthday = [gregorian dateFromComponents:components];
下面的示例显示了如何从一个日历置换到另一个日历:
[objc] view plaincopy
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];

[comps release];
[gregorian release];

NSCalendar *hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *components = [hebrew components:unitFlags fromDate:date];

NSInteger day = [components day]; // 15
NSInteger month = [components month]; // 9
NSInteger year = [components year]; // 5764
历法计算,在当前时间加上一个半小时:
[objc] view plaincopy
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setHour:1];
[offsetComponents setMinute:30];
// Calculate when, according to Tom Lehrer, World War III will end
NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
获得当前星期中的星期天(使用格里高利历)
[objc] view plaincopy
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];

/*
Create a date components to represent the number of days to subtract from the current date.

The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question. (If today is Sunday, subtract 0 days.)
*/
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];

/*
Optional step:
beginningOfWeek now has the same hour, minute, and second as the original date (today).

To normalize to midnight, extract the year, month, and day components and create a new date from those components.
*/
NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: beginningOfWeek];
beginningOfWeek = [gregorian dateFromComponents:components];
如何可以计算出一周的第一天(根据系统的日历设置):
[objc] view plaincopy
NSDate *today = [[NSDate alloc] init];
NSDate *beginningOfWeek = nil;
BOOL ok = [gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:NULL forDate: today];

获得两个日期之间的间隔:
NSDate *startDate = ...;
NSDate *endDate = ...;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];
使用Category来计算同一时代(AD|BC)两个日期午夜之间的天数:
[objc] view plaincopy
@implementation NSCalendar (MySpecialCalculations)

-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate {
NSInteger startDay=[self ordinalityOfUnit:NSDayCalendarUnit inUnit: NSEraCalendarUnit forDate:startDate];

NSInteger endDay=[self ordinalityOfUnit:NSDayCalendarUnit inUnit: NSEraCalendarUnit forDate:endDate];

return endDay-startDay;
}

@end <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
使用Category来计算不同时代(AD|BC)两个日期的天数:
[objc] view plaincopy
@implementation NSCalendar (MyOtherMethod)

-(NSInteger) daysFromDate:(NSDate *) startDate toDate:(NSDate *) endDate {
NSCalendarUnit units=NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comp1=[self components:units fromDate:startDate];
NSDateComponents *comp2=[self components:units fromDate endDate];
[comp1 setHour:12];
[comp2 setHour:12];
NSDate *date1=[self dateFromComponents: comp1];
NSDate *date2=[self dateFromComponents: comp2];
return [[self components:NSDayCalendarUnit fromDate:date1 toDate:date2 options:0] day];
}

@end
判断一个日期是否在当前一周内(使用格里高利历):
[objc] view plaincopy
-(BOOL)isDateThisWeek:(NSDate *)date {

NSDate *start;
NSTimeInterval extends;

NSCalendar *cal=[NSCalendar autoupdatingCurrentCalendar];
NSDate *today=[NSDate date];

BOOL success= [cal rangeOfUnit:NSWeekCalendarUnit startDate:&start interval: &extends forDate:today];

if(!success)
return NO;

NSTimeInterval dateInSecs = [date timeIntervalSinceReferenceDate];
NSTimeInterval dayStartInSecs= [start timeIntervalSinceReferenceDate];

if(dateInSecs > dayStartInSecs && dateInSecs < (dayStartInSecs+extends)){
return YES;
}
else {
return NO;
}
}
获取当前时间
[objc] view plaincopy
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *locationString=[formatter stringFromDate: [NSDate date]];
另外的方法:
[objc] view plaincopy
-(NSString *)getDate
{
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"yyyy-MM-dd EEEE HH:mm:ss a"];
NSString *locationString=[formatter stringFromDate: [NSDate date]];
[formatter release];
return locationString;
}
大写的H日期格式将默认为24小时制,小写的h日期格式将默认为12小时
不需要特别设置,只需要在dataFormat里设置类似”yyyy-MMM-dd”这样的格式就可以了
日期格式如下:
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800
三、使用 NSTimeZone 取得世界各地时间的方法
NSLocale 区域对象封装了文化和语言约定,包括与时间相关的那些约定。
下列程式码将示范,如何利用 NSTimeZone 取得世界上已知的时区名称,并且透过这些名称来获得当地时间,如果在系统时间的取得上有任何疑问,可以参考取得 iOS 系统日期与星期的方法一文,其程式码如下。
[objc] view plaincopy
//取得目前已知的所有地里名称
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];

//取得本地目前时间
NSDate *date = [NSDate date];

for(NSString *name in timeZoneNames) {
NSTimeZone *timezone = [[NSTimeZone alloc] initWithName:name];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

//设定时间格式
[formatter setDateFormat:@"YYYY-MM-d HH:mm:ss"];

//设定时区
[formatter setTimeZone:timezone];

//时间格式正规化并做时区校正
NSString *correctDate = [formatter stringFromDate:date];

NSLog(@"地点:%@ 当地时间:%@",[timezone name], correctDate);

[formatter release];
[timezone release];
}
取得 iOS 系统日期与星期的方法
在之前的文章中已经说明如何取得 Device 里的 iOS 系统时间,在此将在示范如何使用 NSDate 取得系统的日期与星期,请看以下程式码。
[objc] view plaincopy
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate date];

//正规化的格式设定
[formatter setDateFormat:@"YYYY-MM-dd' 'EEEE"];

//正规化取得的系统时间并显示
dateLabel.text = [formatter stringFromDate:date];
当然 NSFormatter 能正规化的格式不只这些,想知道其他的参数可以参考关于 NSDateFormatter 的二三事一文。

3. 取得 iOS 系统时间的方法
如何取得 Device 里的 iOS 系统时间,可以参考以下程式码。
[objc] view plaincopy
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate date];

//正规化的格式设定
[formatter setTimeStyle:NSDateFormatterFullStyle];

//正规化取得的系统时间并显示
timeLabel.text = [formatter stringFromDate:date];
[formatter release];
在时间格式正规化的部份也有多种样式可供选择,其样式如下。
[objc] view plaincopy
[formatter setTimeStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];


美国nsca cscs必须本科吗
美国nsca cscs不用必须是本科学历,没有要求学历条件,是通过提交资料接受审核的。为了缩短通过审核的时间,申请人需在线递交材料以供审核(这些材料包含毕业证书、CPR证书、学生证等等的扫描件或清晰照片)。考试资格审核工作将在收齐申请人材料后的7个工作日内完成。

超越巅峰!EXOS运动表现专家训练课程
Scott 获得运动专业和运动科学专业的学士学位,也是一位NSCA、 ACSM、 NASM认证资格的培训导师。 他经常在健身和运动行业的活动上做演讲。他曾创建了数个全球知名认证体系,学员可以通过网络、书籍和DVD 来学习,比如PTAG。 Scott 为很多顶尖的大学,专业运动队(奥林匹克协会、NFL 和NBA)和大型连锁健身房...

请西安的朋友帮忙!
美格菲健身中心是由香港联交所主板上市公司——迪臣发展国际集团(股票代号0262)之全资公司启康创建集团投资建立的大型会员制运动健身俱乐部。美格菲是国际IHRSA,NSCA,ISPA组织的成员,按照国际流行时尚和康体标准,在中国建立了大规模的健身运动俱乐部连锁企业(武汉、西安、乌鲁木齐、深圳),并向社会提供专业、全...

北京好的健身培训机构有哪些?
3、北京567GO健身学院 北京567GO健身教练培训隶属于北京全能奥菲特健身顾问有限公司,是中国领先的健身教育培训企业,于2005年创建,是体育行业特有职业国家级培训试点单位567GO每年多次进行培训师团队内部考核,为优秀培训师提供出国深造机会。该学校主要设置课程如下:北京健身培训北京全能健身教练培训高级私人教练...

泡泡球的游戏开发者
免费版的Corona SDK可支持用户创建复杂的应用,比如说横向卷轴的物理类游戏。用户如果肯花349美元购买付费版本,还可以在App Store上发行作品。于是,他就自学了Corona所运用的编程语言Lua,完成了这款游戏的编程和美术设计,他的母亲则帮助设计了大部分的关卡。用户在游戏中需要运用自己的技巧将一个小球从...

游泳专项体能训练的作者简介
译者简介:闫琪,现任北京市体育科学研究所副所长、副研究员,长期负责张琳等北京市优秀游泳运动员的科研工作,是美国国家体能协会(NSCA)认证的体能训练专家(CSCS)和私人体能教练(CPT)。近年来致力于体能训练研究,创建BRISS体能训练团队,长期深入训练第一线,在曲棍球、游泳等运动项目的体能训练方面有较...

邕宁县15083465850: 如何使用uidatepicker设置当前系统时间 -
傅飞蛤蚧: 1、创建工程项目,新建一个UIViewController; 2、选中工程,右键-New File…选择“Cocoa Touch Class”-Next,给个合理的名称ViewController,再Next完成; 3、在AppDelegate.m文件包含#import "ViewController.h"; 4、初始化创建...

邕宁县15083465850: 如何创建一个本地时区的Calendar对象?试了几种方法,都只会?
傅飞蛤蚧: 全代码如下: import java.util.Date; import java.util.Calendar; import java.util.GregorianCalendar; public class CustomerApplet extends JApplet implements ...

邕宁县15083465850: Calendar类如何创建对象 为什么? -
傅飞蛤蚧: 一般是这样给Calendar类创建对象的: Calendar cal = Calendar.getInstance();这是因为Calendar类的构造函数是protected,不能直接调用 而Calendar类中的getInstance方法却是静态的,可以通过类名直接引用用上面方法创建出来的Calendar是包含了默认的语言和时区的,想要给calendar赋值可以这样: Data date = new Date(); cal.setTime(date);

邕宁县15083465850: 如何安装php calendar 模块 -
傅飞蛤蚧: 1.进入calendar目录 这个要看你把php的编译包丢那了,我是放在/root/lanmp/php-5.3.27/ext/calendar cd /root/lanmp/php-5.3.27/ext/calendar2.开始编译安装/www/wdlinux/php/bin/phpize./configure --with-php-config=/www/wdlinux/php/bin/php-config ...

邕宁县15083465850: java编程编写一个完成给日期增加指定天数功能的程序 -
傅飞蛤蚧: 可以直接通过Calendar类的add方法进行实现,直接在DAY属性上加上指定的天数就可以了. 举例: Calendar calendar = Calendar.newInstance();//创建一个实例 cd.set(2010,5,24,14,33,22);//实例化一个Calendar. 年、月、日、时、分、秒 calendar.add(Calendar.DAY,1);//给当前日期加上指定天数,这里加的是1天 System.out.println(calendar.getTime());

邕宁县15083465850: 我怎么建立一个NSTimer可以重复两次,然后停止
傅飞蛤蚧: 创建一个静态int在你的计时器委任函数中,初始化为0.每次委任被调用的时候增加它.当计数器达到你期望的值的时候使计时器无效.

邕宁县15083465850: 怎么创建一个HTML文档,在上面放置一个Calendar控件,通过相应的属性设置 -
傅飞蛤蚧: "<html><head><title>时间定时器</title></head><body><div id=""clock""></div></body><script type=""text/javascript"">window.onload=fuction(){window.setInterval(function(){document.getElementById(""clock"").innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());},1000);}</script></html>"

邕宁县15083465850: 如何创建一个只包含年份的nsdate -
傅飞蛤蚧: 1 // 当前时间创建NSDateNSDate *myDate = [NSDate date];NSLog(@"myDate = %@",myDate); 2 //从现在开始的24小时NSTimeInterval secondsPerDay = 24*60*60;NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];NSLog(@"myDate = %@",tomorrow);

邕宁县15083465850: 如何设置 NSDateComponents Weekday 从星期一开始 -
傅飞蛤蚧: [NSCalendar setFirstWeekday:2],改变的是类NSCalendar的week排列顺序,跟NSDateComponents的weekday没有关系,它还是以星期天是1开始,改变的只是NSCalendar的实例,该实例的weekday改为了周一是1开始. 当用NSCalendar的实例传递给NSDateComponents时,传递的是周五,NSDateComponents得到的数据是周五,输出的weekday是6,他们传递的不是weekday的5或者6这个值,而是星期几,这个值. 如果你希望得到周一-周天的顺序,要么用NSCalendar,要么自己再做一次数据处理.

邕宁县15083465850: java如何创建一个指定的日期对象 -
傅飞蛤蚧: import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Demo {public static void main(String[] args) throws ParseException { //获得2009年06月01日 的Date对象 ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网