iOS 获取当前时间及时间戳的互换实例

在项目开发中,难免会遇到使用当前时间,比如实现网络请求上传报文、预约、日历等功能。

1. 获取年月日时分秒

实现代码:

NSDate *date1 = [NSDate date];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateStyle:NSDateFormatterMediumStyle];
[formatter1 setTimeStyle:NSDateFormatterShortStyle];
[formatter1 setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *DateTime1 = [formatter1 stringFromDate:date1];

str就是我们需要的时间,代码中(“YYYY-MM-dd HH:mm:ss”)这个时间的样式是可以根据我们的需求进行修改的,比如:

20170901112253 ==> (“YYYYMMddHHmmss”)

如果只想获取年月,代码如下:

NSDate *date1 = [NSDate date];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateStyle:NSDateFormatterMediumStyle];
[formatter1 setTimeStyle:NSDateFormatterShortStyle];
[formatter1 setDateFormat:@"YYYY-MM"];
NSString *DateTime1 = [formatter1 stringFromDate:date1];

2. 区分系统时间是24小时制还是12小时制

代码如下:

//获取系统是24小时制或者12小时制
NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
NSRange contains = [formatStringForHours rangeOfString:@"a"];
BOOL thisAMPM = contains.location != NSNotFound;

thisAMPM==TURE为12小时制,否则为24小时制

3. 字符串转时间戳

代码如下:

//字符串转时间戳
//datenow为当前时间
NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];
//时间戳的值
NSLog(@"timeSp:%@",timeSp); 

4. 时间戳转字符串

代码如下:

//时间戳转字符串
NSString *timeStr = "1506064573";
NSTimeInterval interval=[timeStr doubleValue] / 1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *currentDateStr = [dateFormatter stringFromDate:date];

以上这篇iOS 获取当前时间及时间戳的互换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • iOS将时间NSDate转化为毫秒时间戳的方法示例

    前言 对于将NSDate类型转换为时间戳,相信大家肯定都会,这样的示例代码,在百度等搜索引擎上面一搜索就是一大篇的东西,但是,大家有没有注意到的是 通过那些方法转换得到的时间戳是 10位的数值,这个数值在转化为 NSDate类型的时候,就会出点儿错,你会发现,每一个时间的 毫秒都是为000的: 错误的毫秒输出 而正确的应该是下面这样的输出: 正确的毫秒输出 好了,接下来就是问题所在了:其实呢,并不是我们代码出错了,而是因为 [[NSDate date] timeIntervalSince1970

  • iOS中时间与时间戳的相互转化实例代码

    本人搜索了很多关于iOS中时间与时间戳的相互转化的资料,下面我来记录一下,有需要了解iOS中时间与时间戳的相互转化的朋友可参考.希望此文章对各位有所帮助. //获取当前系统时间的时间戳 #pragma mark - 获取当前时间的 时间戳 +(NSInteger)getNowTimestamp{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterM

  • iOS NSDate中关于夏令时的坑

    前言 最近线上推广项目的时候,运营反馈了几个bug,其中一个就是字符串转NSDate对象出现nil的情况. 举个例子: NSString *timeStr = @"1992-04-05"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSDate * date1 = [formatter dateFromSt

  • iOS获取当前时间和当前时间戳的方法

    //获取当前的时间 +(NSString*)getCurrentTimes{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //现在时间,你可以输出来看下是什么格式 NSDate *datenow = [NSDate d

  • iOS开发之时间戳(或date)转字符串的实例代码

    1.时间戳转字符串 ///时间戳转化为字符转0000-00-00 00:00 + (NSString *)time_timestampToString:(NSInteger)timestamp{ NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp]; NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init]; [dateFormat setDate

  • IOS 时间和时间戳之间转化示例

    以毫秒为整数值的时间戳转换 时间戳转化为时间NSDate - (NSString *)timeWithTimeIntervalString:(NSString *)timeString { // 格式化时间 NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"]; [formatter setDa

  • iOS 获取当前时间及时间戳的互换实例

    在项目开发中,难免会遇到使用当前时间,比如实现网络请求上传报文.预约.日历等功能. 1. 获取年月日时分秒 实现代码: NSDate *date1 = [NSDate date]; NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init]; [formatter1 setDateStyle:NSDateFormatterMediumStyle]; [formatter1 setTimeStyle:NSDateFormatterSho

  • shell下获取上一个月,星期时间和时间戳的范围实例

    如下所示: #!/bin/bash #一月前 historyTime=$(date "+%Y-%m-%d %H" -d '1 month ago') echo ${historyTime} historyTimeStamp=$(date -d "$historyTime" +%s) echo ${historyTimeStamp} #一周前 $(date "+%Y-%m-%d %H" -d '7 day ago') #本月一月一日 date_th

  • C++ 中时间与时间戳的转换实例详解

    C++ 中时间与时间戳的转换实例详解 // 设置时间显示格式: NSString *timeStr = @"2011-01-26 17:40:50"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [fo

  • ios获取数据之encodeURI和decodeURI的实例

    在APP开发过程中,免不了要进行ios的数据处理,在ios传递数据的过程中,会出现JSON数据获取不到的情况,这时候就轮到encodeURI 和 decodeURI出马了. 1.encodeURI,decodeURI encodeURI:将字符串作为 URI 进行编码 •不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码( 即:- _ . ! ~ * ' ( ) ). •目的是对 URI 全部的编码,因此对在 URI 中具有特殊含义的 ASCII 标点符号(即:;

  • 微信小程序获取当前时间及星期几的实例代码

    效果图如下所示 实例代码如下: util.js function formatTime(date) { var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() return [year, month, d

  • IOS 获取已连接的wifi信息的实现代码

    IOS 获取已连接的wifi信息的实现代码 首先需要   #import <SystemConfiguration/CaptiveNetwork.h> + (id)fetchSSIDInfo { NSArray *ifs = (id)CNCopySupportedInterfaces(); NSLog(@"%s: Supported interfaces: %@", __func__, ifs); id info = nil; for (NSString *ifnam in

  • PHP时间戳格式全部汇总 (获取时间、时间戳)

    PHP语言中的函数有许多种,各种应用方式不同,实现的功能也不尽相同.希望对新手的学习有所帮助! 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2009-1-22″) 结果:1232553600 说明:返回2009年1月22日0点0分0秒时间戳 二,PHP时间戳函数获取英文文本日期时间 示例如下: 便于比较,使用date将当时间戳与指定时间戳转换成系统时间 (1)打印明天此时的时间戳strtot

  • python获取当前时间对应unix时间戳的方法

    本文实例讲述了python获取当前时间对应unix时间戳的方法.分享给大家供大家参考.具体分析如下: Unix timestamp:是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒 import datetime import time print time.mktime(datetime.datetime.now().timetuple()) 输出为: 1431674373.0 PS:这里再为大家推荐一个本站Unix时间戳转换工具,附带了各种语言下Unix时间戳的操作

随机推荐