//
// 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的简单使用的更多相关文章

  1. 【Swift 2.0】实现简单弹幕功能

    前言 简单实现弹幕功能,表跟我谈效率,但也有用队列控制同时弹的数量. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over ...

  2. Swift 2.0 UItableView 的简单使用

    在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方.下面我先把整个控制器的 ...

  3. swift 3.0字符串的简单使用

    let str:String = "12314124" 获取某个指定位置的元素 print(str.characters[str.index(str.startIndex, off ...

  4. Swift互用性:与 Cocoa 数据类型共舞(Swift 2.0版)-b

    本节内容包括: 字符串(Strings) 数值(Numbers) 集合类(Collection Classes) 错误(Errors) Foundation数据类型(Foundation Data T ...

  5. Swift 3 关于Date的一些操作

    前言 最近在写关于日期的一些操作,所以整理了一下这方面的一些知识 本Demo使用的是playground. 我们以前使用的都是NSDate类进行日期的操作,在Swift 3.0中,我们就可以使用更加S ...

  6. Swift 2.0 异常处理

    转自:http://www.jianshu.com/p/96a7db3fde00 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主 ...

  7. fir.im Weekly - Swift 3.0 的迁移适配指南

    无论你是移动开发者,还是桌面端开发者,或者正在IoT领域探索的技术人员,那么应该更加关注 iDev 全平台开发者大会,也许是后半年 iOS 开发者最盛大的技术盛宴.既有知名公司带来专业视野,又有从 S ...

  8. Swift 3.0 令人兴奋,但Objective-C也有小改进--Objective-C的类属性

    由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.或许你会觉得苹果提及Objective-C 很可能是为了提高和Swift互操作性(译者注:互操作性主 ...

  9. Swift 2.0初探

    转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心. 今年6月,一年一度的WWDC大会如期而至,在大会上Apple发布了Swift 2.0,引 ...

随机推荐

  1. 【JOISC2012 / bzoj4388】Invitation

    Description link Solution 可以发现题目在模拟 Prim 算法求最大生成树的过程,树边故答案与起点 \(C\) 无关. 先把所有区间离散化,注意对于一个区间 \([l,r]\) ...

  2. 用tensorflow实现SVM

    环境配置 win10 Python 3.6 tensorflow1.15 scipy matplotlib (运行时可能会遇到module tkinter的问题) sklearn 一个基于Python ...

  3. 从Excel中读取数据并批量写入MySQL数据库(基于MySQLdb)

    一.Excel内容如下,现在需要将Excel中的数据全部写入的MySQL数据库中: 二.连接MySQL的第三方库使用的是“MySQLdb”,代码如下: # -*- coding:utf-8 -*-im ...

  4. FreeRTOS任务优先级说明

    https://www.cnblogs.com/yangguang-it/p/7156445.html#4217682 下面对 FreeRTOS 优先级相关的几个重要知识点进行下说明,这些知识点在以后 ...

  5. 使用PHP读取PHP文件并输出到屏幕上

    看完这篇文章,你一定忘不掉htmlentities的用法 背景 今天有个需求,就是一个PHP开发的网址中,有一个页面可以提供给用户修改已经存在的PHP文件中的代码,并POST到服务器上保存. 每次将读 ...

  6. PHP mysqli_get_connection_stats() 函数

    定义和用法 mysqli_get_connection_stats() 函数返回有关客户端连接的统计. 语法 mysqli_get_connection_stats(connection); 返回有关 ...

  7. Windows:获取本地时间

    造冰箱的大熊猫@cnblogs 2019/6/4 #include <windows.h> int func() { SYSTEMTIME systime; GetLocalTime ( ...

  8. 小米oj 有多少个等差数列(动态规划)

     有多少个等差数列? 序号:#20难度:困难时间限制:500ms内存限制:10M 描述 等差数列是常见数列的一种,如果一个数列从第二项起,每一项与它的前一项的差等于同一个常数,这个数列就叫做等差数列, ...

  9. 微信小程序底层原理与运行机制类文章学习

    参考文档 小程序底层实现原理及一些思考 为了安全和管控, 双线程执行 Web Worker执行用户的代码; UI线程执行大部分的功能. 微信小程序架构原理 只通过mvvm模板语法动态改变页面, 不支持 ...

  10. ACM之路(13)—— 树型dp

    最近刷了一套(5题)的树型dp题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=116767#overview,算是入了个门,做下总结. ...