Swift 3.0 Date的简单使用
//
// ViewController.swift
// Date的使用
//
// Created by 思 彭 on 16/9/20.
// Copyright © 2016年 思 彭. All rights reserved.
// import UIKit class ViewController: UIViewController { override func viewDidLoad() {
super.viewDidLoad() /// 1.获得当前的日期和时间: currnetDate = 2016-09-20 02:22:22 +0000
let currentDate = Date()
print("currnetDate = \(currentDate)") // 2.初始化DateFormatter类
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current() // 3.date转string :带样式输出日期: dateString =
dateFormatter.dateStyle = DateFormatter.Style.noStyle
let dateString = dateFormatter.string(from: currentDate)
print("dateString = \(dateString)") // date1String = 9/20/16
dateFormatter.dateStyle = DateFormatter.Style.shortStyle
let date1String = dateFormatter.string(from: currentDate)
print("date1String = \(date1String)") // date2String = Tuesday, September 20, 2016
dateFormatter.dateStyle = DateFormatter.Style.fullStyle
let date2String = dateFormatter.string(from: currentDate)
print("date2String = \(date2String)") // date3String = September 20, 2016
dateFormatter.dateStyle = DateFormatter.Style.longStyle
let date3String = dateFormatter.string(from: currentDate)
print("date3String = \(date3String)") // date4String = Sep 20, 2016
dateFormatter.dateStyle = DateFormatter.Style.mediumStyle
let date4String = dateFormatter.string(from: currentDate)
print("date4String = \(date4String)") // 4.自定义输出格式 date5Str = Tuesday, September, 20, 2016
dateFormatter.dateFormat = "EEEE, MMMM, dd, yyyy"
let date5Str = dateFormatter.string(from: currentDate)
print("date5Str = \(date5Str)") // 5. string转date date = Optional(2016-12-02 10:15:59 +0000) let str = "2016-12-02 18:15:59"
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.date(from: str)
print("date = \(date!)")// 加上! 输出: date = 2016-12-02 10:15:59 +0000 // 6.DateCopmonents的使用
// era:1 year:2016 month:9 day: 20 hour:10 minute:53 second: 0
let currentCalendar = Calendar.current()
let dateComponents = currentCalendar.components([Calendar.Unit.era,Calendar.Unit.year,Calendar.Unit.month,Calendar.Unit.day,Calendar.Unit.hour,Calendar.Unit.second,Calendar.Unit.minute], from: currentDate)
print("era:\(dateComponents.era!) year:\(dateComponents.year!) month:\(dateComponents.month!) day: \(dateComponents.day!) hour:\(dateComponents.hour!) minute:\(dateComponents.minute!) second: \(dateComponents.second!)") // 7.DateComponents -> Date
// compents = 2016-09-19 16:00:00 +0000
var componrnts = DateComponents()
componrnts.year =
componrnts.month =
componrnts.day =
let compents = currentCalendar.date(from: componrnts)
print("compents = \(compents!)"); // 8.比较日期和时间
dateFormatter.dateFormat = "MMM dd, yyyy zzz"
let str1 = "May 08, 2016 GMT"
let date1 = dateFormatter.date(from: str1)
let str2 = "May 10, 2016 GMT"
let date2 = dateFormatter.date(from: str2) // date1:Optional(2016-05-08 00:00:00 +0000)----date2:Optional(2016-05-10 00:00:00 +0000)
print("date1:\(date1)----date2:\(date2)") // 2016-05-08 00:00:00 +0000 2016-05-10 00:00:00 +0000
print((date1! as NSDate).earlierDate(date2!)); //日期更早的NSDate对象
print((date1! as NSDate).laterDate(date2!)); //日期更晚的NSDate对象 if date1?.timeIntervalSinceReferenceDate > date2?.timeIntervalSinceReferenceDate {
print("date1 is later")
} else if date1?.timeIntervalSinceReferenceDate < date2?.timeIntervalSinceReferenceDate {
print("date2 is later")
} else if date1?.timeIntervalSinceReferenceDate == date2?.timeIntervalSinceReferenceDate {
print("Same Date")
} // 9. 日期的计算
let monthsToAdd =
let daysToAdd = var calculatedDate = currentCalendar.date(byAdding: Calendar.Unit.month, value: monthsToAdd, to: currentDate, options: Calendar.Options.init(rawValue: ))
calculatedDate = currentCalendar.date(byAdding: Calendar.Unit.day, value: daysToAdd, to: calculatedDate!, options: Calendar.Options.init(rawValue: ))
print(calculatedDate) // Optional(2016-10-24 08:33:41 +0000)
} }
Swift 3.0 Date的简单使用的更多相关文章
- 【Swift 2.0】实现简单弹幕功能
前言 简单实现弹幕功能,表跟我谈效率,但也有用队列控制同时弹的数量. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over ...
- Swift 2.0 UItableView 的简单使用
在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方.下面我先把整个控制器的 ...
- swift 3.0字符串的简单使用
let str:String = "12314124" 获取某个指定位置的元素 print(str.characters[str.index(str.startIndex, off ...
- Swift互用性:与 Cocoa 数据类型共舞(Swift 2.0版)-b
本节内容包括: 字符串(Strings) 数值(Numbers) 集合类(Collection Classes) 错误(Errors) Foundation数据类型(Foundation Data T ...
- Swift 3 关于Date的一些操作
前言 最近在写关于日期的一些操作,所以整理了一下这方面的一些知识 本Demo使用的是playground. 我们以前使用的都是NSDate类进行日期的操作,在Swift 3.0中,我们就可以使用更加S ...
- Swift 2.0 异常处理
转自:http://www.jianshu.com/p/96a7db3fde00 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主 ...
- fir.im Weekly - Swift 3.0 的迁移适配指南
无论你是移动开发者,还是桌面端开发者,或者正在IoT领域探索的技术人员,那么应该更加关注 iDev 全平台开发者大会,也许是后半年 iOS 开发者最盛大的技术盛宴.既有知名公司带来专业视野,又有从 S ...
- Swift 3.0 令人兴奋,但Objective-C也有小改进--Objective-C的类属性
由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.或许你会觉得苹果提及Objective-C 很可能是为了提高和Swift互操作性(译者注:互操作性主 ...
- Swift 2.0初探
转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心. 今年6月,一年一度的WWDC大会如期而至,在大会上Apple发布了Swift 2.0,引 ...
随机推荐
- linux和unix下crontab的使用
在LINUX中,周期执行的任务一般由cron这个守护进程来处理 [ps -ef | grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为 ...
- golang restful api
https://medium.com/@petrousov/how-to-build-a-restful-api-in-go-for-phonebook-app-d55f7234a10 ------- ...
- Oracle 连接排序
---左联操作SELECT e.* FROM hs_opt_ewb e left join hs_workform_main m on e.ewb_no=m.ewb_nowhere e.ewb_no= ...
- poj2942 Knights of the Round Table[点双+二分图染色]
首先转化条件,把无仇恨的人连边,然后转化成了求有哪些点不在任何一个奇环中. 一个奇环肯定是一个点双,所以想到处理出所有点双,但是也可能有的点双是一个偶环,有的可能是偶环和奇环混杂,不好判. 考察奇环性 ...
- django + ckeditor + 七牛云,图片上传到七牛云
传送门 本人使用的是 Django 的自带的管理后台,安装 ckeditor 富文本编辑器后,上传图片的时候直接传到七牛云的.
- [Spring Boot] Set Context path for application in application.properties
If you were using Microservice with Spring Boot to build different REST API endpoints, context path ...
- 解决oracle 32位64位的问题
工具-选项-项目和解决方案-勾选第一个
- FPGA数据舍入方式
1,在Verilog代码中,常用的代码写法为直接截位: 2,在Vivado的IP核中常见的两种舍入方式为Truncation和Rounding, 3,在Matlab中常见的四种舍入函数为floor, ...
- 51 Nod 1110距离之和最小V3
1110 距离之和最小 V3 1 秒 131,072 KB 40 分 4 级题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 * ...
- Cogs 14. [网络流24题] 搭配飞行员(二分图匹配)
[网络流24题] 搭配飞行员 ★★☆ 输入文件:flyer.in 输出文件:flyer.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 飞行大队有若干个来自各地的驾驶员,专门 ...