iOS Development time Processing tool 01

Source: Internet
Author: User

1. Get the current time
/** 获取当前的时间 */+(NSString*)getCurrentTimes{    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];    //现在时间,你可以输出来看下是什么格式    NSDate *datenow = [NSDate date];    //----------将nsdate按formatter格式转成nsstring    NSString *currentTimeString = [formatter stringFromDate:datenow];    NSLog(@"currentTimeString =  %@",currentTimeString);    return currentTimeString;}
2. There are two ways to get the current timestamp (in seconds)
/** 方法一 */+(NSString *)getNowTimeTimestamp{    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;    [formatter setDateStyle:NSDateFormatterMediumStyle];    [formatter setTimeStyle:NSDateFormatterShortStyle];    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制    //设置时区,这个对于时间的处理有时很重要    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];    [formatter setTimeZone:timeZone];    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];    return timeSp;}/** 方法二 */+(NSString *)getNowTimeTimestamp2{    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];    NSTimeInterval a=[dat timeIntervalSince1970];    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型    return timeString;}
3. Get the current timestamp (in milliseconds)
/** 获取当前时间戳  (以毫秒为单位)*/+(NSString *)getNowTimeTimestamp3{   NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;   [formatter setDateStyle:NSDateFormatterMediumStyle];   [formatter setTimeStyle:NSDateFormatterShortStyle];   [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"];   // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];   [formatter setTimeZone:timeZone];   NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式   NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];   return timeSp; }

iOS Development time Processing tool 01

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.