方式一:XXXX年-XX月-XX日  XX时:XX分:XX秒的格式

- (IBAction)LoginAction:(UIButton *)sender

{

NSDate *date = [NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *DateTime = [formatter stringFromDate:date];
    NSLog(@"%@============年-月-日  时:分:秒=====================",DateTime);

}

方式二:XXXX年-X月-X日  XX时:XX分:XX秒的格式  
- (IBAction)LoginAction:(UIButton *)sender{

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 =(int) [dateComponent year];
    int month = (int) [dateComponent month];
    int day = (int) [dateComponent day];
    int hour = (int) [dateComponent hour];
    int minute = (int) [dateComponent minute];
    int second = (int) [dateComponent second];
    
    NSInteger year = [dateComponent year];
    NSInteger month =  [dateComponent month];
    NSInteger day = [dateComponent day];
    NSInteger hour =  [dateComponent hour];
    NSInteger minute =  [dateComponent minute];
    NSInteger second = [dateComponent second];

NSLog(@"year is: %ld", (long)year);
    NSLog(@"month is: %ld", (long)month);
    NSLog(@"day is: %ld", (long)day);
    NSLog(@" hour is: %ld",  (long)hour);
    NSLog(@"minute  is: %ld", (long)minute );
    NSLog(@"second is: %ld", (long)second);
     //字符串的转化并且拼接
      NSString *yearstr=[NSString stringWithFormat:@"%ld-",(long)year];
      NSString *monthstr=[NSString stringWithFormat:@"%ld-",(long)month];
     NSString *daystr=[NSString stringWithFormat:@"%ld ",(long)day];
     NSString *hourstr=[NSString stringWithFormat:@"%ld:",(long)hour];
    NSString *minutestr=[NSString stringWithFormat:@"%ld:",(long)minute];
    NSString *secondstr=[NSString stringWithFormat:@"%ld",(long)second];
    //字符串开始拼接
    NSString *allstr=[yearstr stringByAppendingString:monthstr];
    NSString *allstr1=[allstr stringByAppendingString:daystr];
    NSString *allstr2=[allstr1 stringByAppendingString:hourstr];
    NSString *allstr3=[allstr2 stringByAppendingString:minutestr];
    NSString *DateTime=[allstr3 stringByAppendingString:secondstr];
    NSLog(@"最后年月日时分秒拼接的结果=====%@",DateTime);
}

转载于:https://www.cnblogs.com/dongiosblogs/p/4612749.html

获取当前时间---年月日时分秒------iOS相关推荐

  1. HTML获取当前时间年月日时分秒等相关信息

    HTML获取当前时间年月日时分秒等相关信息 [方式一]获取整段时间 <!DOCTYPE html> <html lang="en"> <head> ...

  2. java如何获取当前时间 年月日 时分秒

    需要导包 import java.text.SimpleDateFormat; import java.util.*; 获取当前时间,并格式化为(年-月-日 时:分:秒). Date date = n ...

  3. js 获取当前日期时间 年月日 时分秒

    一.获取当前日期方法 // 格式化日对象 const getNowDate = () => {var date = new Date();var sign2 = ":";va ...

  4. js 获取当前日期(年月日时分秒周)

    js 获取当前日期(年月日时分秒周) var myDate = new Date(); var myYear = myDate.getFullYear(); // 获取当前年份 var myMonth ...

  5. js获取当前的年月日时分秒

    <!DOCTYPE html> <html><head><meta charset="UTF-8"><title>< ...

  6. oracle 当前时间年月日时分秒,oracle存储精确时间 年月日 时分秒

    我用的是ibatis框架 为了做一个聊天功能,发现不能显示最新的消息,一看,原来oracle中存入的时间不对 oracle中要存入 年月日 时分秒首先要把 oracle中这列设置为 timestamp ...

  7. oracle 当前时间年月日时分秒,oracle获取当前年月日时分秒季度周

    oracle中如何获取系统当前时间 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; ORACLE里获取一个时间的年.季.月.周.日 ...

  8. input 时分秒输入_JavaScript实现input框获取系统默认年月日时分秒

    这是对旧的已上线项目的维护之中优化的一个部分,要求是在input框里面默认当前系统的时间.实现如下图所示的效果. 我这里用的是一款日历插件:jedate-开始使用一款好用的时间插件:https://w ...

  9. Android自定义控件动态显示时间 年月日 时分秒

    这个要求用的不多,但是也有写项目上面不想看手机时间,要求你直接把时间日期给动态显示在界面上 这里就记录一个自定义控件吧 public class UpdateTimeTextView extends ...

最新文章

  1. 人工智能助力全国大学智能车竞赛
  2. 数据有了,如何构建数据资产?
  3. windows下安装rabbitMQ教程(实战书写)
  4. 你需要熟练运用的12个命令行工具
  5. miui8.2 是android 7.0,因与MIUI 8.2撞车 小米5暂缺失安卓7.0
  6. OFFICE技术讲座:边框的3D与方框的区别
  7. Gym 101775 D (思维)
  8. reportlab 应用 打印考生成绩
  9. 企业微信绑定企业邮箱服务器配置,企业微信企业邮箱开通规则说明介绍
  10. 用微信公众号做淘宝优惠券查券搜券和返利机器人的详细配置教程
  11. HR问:“对我们公司你有什么问题要问的吗”,怎样回答才算完美!
  12. 页面显示\n\tat的问题
  13. JQuery Ajax使用FormData对象上传文件 图片
  14. 若依前后端分离版:增加新的登录接口,用于小程序或者APP获取token,并使用若依的验证方法
  15. MongoDB面试问题
  16. 友价实现一键Nofollow(数据库替换)
  17. 傲游 android 2.3,傲游浏览器安卓版-傲游浏览器手机版v5.2.3.3256-3454手机软件
  18. Amazon CloudWatch 介绍/学习
  19. 网络舆论舆情监测怎么做的系统技术解决办法
  20. Netty游戏服务器实战开发(12):线程任务组件开发

热门文章

  1. 解决vmware“二进制转换与此平台长模式不兼容.....”的问题
  2. 标记偏置 隐马尔科夫 最大熵马尔科夫 HMM MEMM
  3. linux raid5卷,Linux逻辑卷及RAID5的创建
  4. python 类可以调用实例变量_Python实例方法、类方法、静态方法区别详解
  5. 系统学习深度学习(四十二)--从AE到VAE
  6. Android NDK开发之 opencv for android 问题总结
  7. osea/ 5.0-6.0
  8. Nacos教程_3 整合SpringCloud(配置中心+服务发现)
  9. Vue-Cli3 使用jquery
  10. mysql 下载教程_MySQL下载安装详情图文教程