NSDate 格式化 及 互转
/*
NSDateFormatter的作用
1.NSString -> NSDate
2.NSDate -> NSString
*/
void fmt_date_to_string();
void fmt_string_to_date();
void fmt_string_to_date2();
void fmt_timestamp_to_date3(); int main(int argc, const char * argv[]) {
@autoreleasepool {
// 2015-10-01 14:38:40
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createdAtDate = [fmt dateFromString:@"2015-9-15 23:59:59"]; NSCalendar *calendar = [NSCalendar currentCalendar];
NSLog(@"%d", [calendar isDateInTomorrow:createdAtDate]);
}
return ;
} void calendar_interval_between_date()
{
// 获得系统当前时间
NSDate *nowDate = [NSDate date]; // 服务器返回的时间字符串
NSString *createdAtString = @"2014-02-20 10:49:54";
// 创建一个日期格式化对象
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// NSString -> NSDate
NSDate *createdAtDate = [fmt dateFromString:createdAtString]; // 计算createdAtDate和nowDate的时间间隔
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *cmps = [calendar components:unit fromDate:createdAtDate toDate:nowDate options:];
NSLog(@"%@", cmps);
} /**
* 获得某个NSDate对象的所有日期元素 : 年月日时分秒
*/
void get_components_of_date()
{
// 获得系统当前时间
NSDate *nowDate = [NSDate date]; // 日历对象
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *cmps = [calendar components:unit fromDate:nowDate];
NSLog(@"%@", cmps); // NSInteger year = [calendar component:NSCalendarUnitYear fromDate:nowDate];
// NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:nowDate];
// NSLog(@"%zd %zd", year, minute); // NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// // 设置日期格式
// fmt.dateFormat = @"HH";
// NSString *string = [fmt stringFromDate:nowDate];
// NSLog(@"%@", string);
} /**
* 2个NSDate之间的时间差值
*/
void date_interval()
{
// 获得系统当前时间
NSDate *nowDate = [NSDate date]; // 服务器返回的时间字符串
NSString *createdAtString = @"2015-10-16 10:49:54";
// 创建一个日期格式化对象
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// NSString -> NSDate
NSDate *createdAtDate = [fmt dateFromString:createdAtString]; // 计算createdAtDate和nowDate之间相隔的秒数
NSTimeInterval interval = [nowDate timeIntervalSinceDate:createdAtDate];
NSLog(@"%f", interval);
// 3600 / 60 = 60
// 60 /60 = 1
} /**
* timestamp -> NSDate
*/
void fmt_timestamp_to_date3()
{
// 时间戳 : 从1970年1月1日0点0分0秒开始经历的毫秒数
NSInteger timestamp = ; NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0]; NSLog(@"%@", date);
} /**
* NSString -> NSDate
*/
void fmt_string_to_date2()
{
// 服务器返回的时间字符串
NSString *string = @"Tue May 31 17:46:55 +0800 2011"; // 创建一个日期格式化对象
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 解析欧美格式的日期字符串, 得设置语言类型为en_US
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // 设置日期格式
fmt.dateFormat = @"EEE MMM dd HH:mm:ss ZZZZ yyyy"; NSDate *date = [fmt dateFromString:string]; NSLog(@"%@", date);
} /**
* NSString -> NSDate
*/
void fmt_string_to_date()
{
// 服务器返回的时间字符串
NSString *string = @"2015-10-16 10:49:54"; // 创建一个日期格式化对象
NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; // 设置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *date = [fmt dateFromString:string]; NSLog(@"%@", date);
} /**
* NSDate -> NSString
*/
void fmt_date_to_string()
{
// 获得系统当前时间
NSDate *now = [NSDate date]; // 创建一个日期格式化对象
NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; // 设置日期格式 : 2015年10月10日 15时56分30秒
// 年(year) : y
// 月(month) : M
// 日(day) : d
// 时(hour) : H(24小时制)\h(12小时制)
// 分(minute) : m
// 秒(second) : s
// 时区(zone) : Z fmt.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒"; // NSDate -> NSString
NSString *nowString = [fmt stringFromDate:now]; NSLog(@"----- %@", nowString);
}
NSDate 格式化 及 互转的更多相关文章
- NSDate 格式化 NSDate to NSString
NSLog(@"%@",[NSDate stringFromDate:[NSDate date] withFormat:@"yyyyMMdd__HH_mm_ss_zzz& ...
- NSDate 格式化含有毫秒
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; 版权声明:本文为博主原创文章,未经博主允许不得转载.
- iOS中NSDate常用转换操作整合
//当前时间格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss + (NSString *)getCurrentDataWithDateFormate:(NSString *)format ...
- oracle常规使用(一)
目录 特殊sql distinct 项目中遇到表中无主键,但是某个字段不能重复. 需要匹配id串里的内容 批量更新,但是批量成功返回的是-1 时间格式化 行列互转 应用场景 列转行 总结 oracle ...
- 时间戳和LocalDateTime和Date互转和格式化
一 前言 续上篇java8在日常开发中使用LocalDate和LocalTime[https://blog.csdn.net/youku1327/article/details/102771936]中 ...
- iOS NSDate获取当前时间并格式化
NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM- ...
- day04-交互、格式化输出及基本运算符
目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...
- NSString 常见数据类型转换:转NSInteger , NSDate(互转)
1. NSString转NSInteger, 转int (float, double类似 ) 1.1正常情况 , NSString所包含内容确能转化为int的类型 NSString *sNumber ...
- DAY04 与用户交 互格式化输出与运算符
与用户交互 输入: input # python2与python3的区别 # python3 res = input('please in put your username>>>& ...
随机推荐
- JavaScript(八)日期对象
Date对象 1.创建方式 var now = new Date(); //现在返回的直接就是 当前的时间 不需要进行换算了 返回格式 (星期 月 日 年 时 分 秒 时区) 2.日期的格式化方 ...
- 生成Nuget 源代码包来重用你的Asp.net MVC代码
ASP.NET 开发人员有时会陷入一种困境:想要重用以前写过的东西,如一些具有完整功能的Web页面+后台逻辑, 往往不那么直接了当,因此很不爽.经常采用的方式是:找到以前写过的项目,从中挑出来一些有用 ...
- 机器学习_K近邻Python代码详解
k近邻优点:精度高.对异常值不敏感.无数据输入假定:k近邻缺点:计算复杂度高.空间复杂度高 import numpy as npimport operatorfrom os import listdi ...
- C: 当字符数组首指针转化成char *指针,sizeof(*ptr)不为array的size
#include <stdio.h> #include <string.h> int main() { char a[10] = "\0"; char *p ...
- 我的MYSQL学习心得链接
http://www.cnblogs.com/lyhabc/p/3793524.html
- git 的 基础操作及使用
/* git svn版本控制器 */ /*git把文件对应的储存空间分为三个区: 1.工作区 2.缓存区 3.历史区 直接操作文件,不做add时,咱们是在工作区做的修改 右键 git bash her ...
- React-native SyntaxError: Unexpected token ...
更新 node.js 版本到 v6.11.1. https://github.com/facebook/react-native/issues/15040
- 解决idea控制台打印乱码问题
idea控制台打印乱码,用起来总别扭,也是在网上搜索了一番,靠一点猜测解决了. 首先打开你自己的idea的安装目录下(即右键桌面图标,点击打开文件所在位置),然后找到idea.exe.vmoption ...
- Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
在连接数据库时,使用了最新版本的mysql-Connector,所以导致老版本的“com.mysql.jdbc.Drive”不可行,要改为“com.mysql.cj.jdbc.Driver”
- python之cookbook-day03
第一章:数据结构和算法 1.3 保留最后 N 个元素 问题: 在迭代操作或其他操作的时候,怎样只保留最后有限几个元素的历史记录? 解决方案: 保留有限历史记录正是 collections.deque ...