IOS开发-OC获取当前时间戳

//获取当前时间戳
-(NSString *) getTimeNow{
    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 *timeStamp = [NSString stringWithFormat:@"%ld", (long)[dateNow timeIntervalSince1970]];
    return timeStamp;
}

 

posted @ 2023-07-19 11:39  SadicZhou  阅读(1823)  评论(0编辑  收藏  举报