NSDate *  senddate=[NSDate date];  

NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];  

[dateformatter setDateFormat:@"YYYYMMdd"];  

NSString *  locationString=[dateformatter stringFromDate:senddate];    

NSLog(@"locationString:%@",locationString);

 [dateformatter release]; //这个有问题,显示日期不对

关大叔的写法

    //获取当前时间
NSDate *now = [NSDate date];
NSLog(@”now date is: %@”, now); NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now]; int year = [dateComponent year];
int month = [dateComponent month];
int day = [dateComponent day];
int hour = [dateComponent hour];
int minute = [dateComponent minute];
int second = [dateComponent second]; NSLog(@”year is: %d”, year);
NSLog(@”month is: %d”, month);
NSLog(@”day is: %d”, day);
NSLog(@”hour is: %d”, hour);
NSLog(@”minute is: %d”, minute);
NSLog(@”second is: %d”, second);

获取星期几(好用的)可以获取:年、月、日、星期几

   NSDate *datet = [NSDate date];//现在时间
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"Sunday", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil]; NSCalendar *calendars = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"]; [calendars setTimeZone: timeZone]; NSCalendarUnit calendarUnit = NSCalendarUnitWeekday; NSDateComponents *theComponents = [calendars components:calendarUnit fromDate:datet]; NSInteger unitFlagss = NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitWeekday |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond; theComponents = [calendars components:unitFlagss fromDate:datet];
int week = [theComponents weekday];
int year=[theComponents year];
int month = [theComponents month];
int day = [theComponents day]; NSLog(@"%d年%d月",year,month);
NSLog(@"%d",day); NSLog(@"xingqi%@",[weekdays objectAtIndex:theComponents.weekday]);

自学iOS-获取当前时间的更多相关文章

  1. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  2. iOS 获取当前时间格式化字符串

    iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...

  3. iOS - 获取当前时间日期星期几

    //获取当前时间日期星期 - (NSString *)getCurrentTimeAndWeekDay { NSArray * arrWeek=[NSArray arrayWithObjects:@& ...

  4. iOS获取网络时间与转换格式

      [NSDate date]可以获取系统时间,但是会造成一个问题,用户可以自己修改手机系统时间,所以有时候需要用网络时间而不用系统时间.获取网络标准时间的方法: 1.先在需要的地方实现下面的代码,创 ...

  5. iOS 获取当前时间以及计算年龄(时间差)

    获取当前时间 NSDate *now = [NSDate date]; NSLog(@"now date is: %@", now); NSCalendar *calendar = ...

  6. iOS获取本地时间

    NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter a ...

  7. ios获取系统时间

    //获取系统时间 NSDate * date=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; ...

  8. iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法

    //获取当前时间 NSDate *now = [NSDate date]; NSLog(@" now date is: %@ ",now); NSCalendar *calenda ...

  9. iOS获取当前时间

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时 ...

  10. IOS获取系统时间 NSDate

    //返回当前时间,精确到毫秒.- (NSString *)getTimeNow { NSString* date; NSDateFormatter * formatter = [[NSDateForm ...

随机推荐

  1. Linux内核模块Makefile学习

    在<Linux设备驱动程序>一书中读到的内核模块编译Makefile,不是非常理解,在查询很多资料后,在这里做个总结. 书中Makefile代码: ifneq ($(KERNELRELEA ...

  2. WPF RichTextBox 自定义文字转超链接

    搬运自StackOverflow private void AddHyperlinkText(string linkURL, string linkName, string TextBeforeLin ...

  3. msserver的update or insert语句

    方案1:SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; IF EXISTS (SELECT 1 FROM dbo.ta ...

  4. VBA中字符串连接/字符串拼接中“&”和“+”的区别

    VBA中字符串连接/字符串拼接中“&”和“+”的区别   在VBA中用于字符串连接的只有“&”和“+”两种运算符. 1.“&”是强制性连接,就是不管什么都连接. 2.“+”是对 ...

  5. c++ goto语句

    #include <stdio.h> #include <math.h> int main(void) //main是程序入口 { int num; printf(" ...

  6. PCA人脸识别的python实现

    这几天看了看PCA及其人脸识别的流程,并在网络上搜相应的python代码,有,但代码质量不好,于是自己就重新写了下,对于att_faces数据集的识别率能达到92.5%~98.0%(40种类型,每种随 ...

  7. 2_C语言中的数据类型 (一)2.1.常量和字符串常量

    2.1 常量就是在程序中不可变化的量,常量在定义的时候必须给一个初值. 1.1.1          #define 定义一个宏常量 1.1.2          const 定义一个const常量 ...

  8. Python_sklearn机器学习库学习笔记(六) dimensionality-reduction-with-pca

    # 用PCA降维 #计算协方差矩阵 import numpy as np X=[[2,0,-1.4], [2.2,0.2,-1.5], [2.4,0.1,-1], [1.9,0,-1.2]] np.c ...

  9. Codeforces 909 D. Colorful Points (模拟)

    题目链接: Colorful Points 题意: 给出一段字符串(长度最大为1e6),每次操作可以删除字符串中所有相邻字符与其不同的字符.例如:aabcaa 删除一次就变成了aa,就无法再删除了.题 ...

  10. Python distribute到底使用package_data还是MANIFEST.in?

    今天看Flask的文档,里面提到如何通过distribute发布你自己的Python包.讲包含文件的时候,提到要用MANIFEST.in并将include_package_data设置为True. 由 ...