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,引 ...
随机推荐
- java线程基础巩固---创建并启动线程
对于java的并发编程方面的东东,不管是面试还是实际工作开发都是非常重要的,而往往只要涉及到并发相关的东东有点让人觉得有点难,而实际工作中涉及到并发可能就是简单的用下同步块.上锁之类的一些简单的操作, ...
- spring 的自动定时任务
spring的自动定时任务有两种 第一种:通过xml配置来设置 需要在xml中引入新的约束,并且需要配置<task:scheduled-tasks> ,主要配置内容如下: <?xml ...
- C# UdpClient使用
客户端: public class UdpClientManager { //接收数据事件 public Action<string> recvMessageEvent = null; / ...
- 如何使用NugetPackageExplorer 创建Nuget发布包,简易版
在上一篇博客中,详细介绍了个人Nuget服务器的搭建.这篇博客中,将详细介绍一下如何使用NugetPackageExplorer工具制作可以发布到Nuget服务器上包. 直奔主题 在开始之前,需要下载 ...
- BZOJ 3932: [CQOI2015]任务查询系统 (主席树板题)
就是裸的主席树,差分之后排序插入主席树就行了. 注意主席树查询的时候叶子节点要特判,因为本身是有size的 还有要开longlong CODE #include <cctype> #inc ...
- python镜像
国内镜像列表豆瓣: http://pypi.doubanio.com/simple清华: https://pypi.tuna.tsinghua.edu.cn/simple科大: https://mir ...
- TabSheet源码
TabSheet.h #if !defined(AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_) #define AFX ...
- 根据xml文件生成javaBean
原 根据xml文件生成javaBean 2017年08月15日 18:32:26 吃完喝完嚼益达 阅读数 1727 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出 ...
- 洛谷P4979 矿洞:坍塌
洛谷题目链接 珂朵莉树吼啊!!! 又是一道水题,美滋滋~~~ $A$操作完全模板区间赋值 $B$操作也是一个模板查询,具体看代码 注意:读入不要用$cin$,会$T$,如果你是大佬,会玄学东西当我没说 ...
- c 判断一个字符是否为字母
#include <stdio.h> #include <wctype.h> int main () { ; wchar_t str[] = L"C++"; ...