当前位置:网站首页>日期转换 EEE MMM dd HH:mm:ss zzz yyyy
日期转换 EEE MMM dd HH:mm:ss zzz yyyy
2022-07-28 23:52:00 【Hanyang Li】
1. 创建日期转换分类
//
// Date+Extension.swift
//
import Foundation
extension Date{
/// 将格式的字符串转换成日期
static func fromData(string: String) ->Date?{
let df = DateFormatter()
df.locale = Locale(identifier: "en")
df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
return df.date(from: string)
}
/**
返回当前日期多描述信息
刚刚(一分钟内)
x分钟前(一小时内)
x小时前(当天)
昨天 HH:mm(昨天)
MM-dd HH:mm(一年内)
yyyy-MM-dd HH:mm(更早期)
*/
var dateDescription: String{
//取出当前日历 - 提供了大量的日历相关的操作函数
let calendar = Calendar.current
//处理今天的日期
if calendar.isDateInToday(self) {
let delta = Int(Date().timeIntervalSince(self))
if delta < 60 {
return "刚刚"
}
if delta < 3600{
return "\(delta/60) 分钟前"
}
return "\(delta/3600) 小时前"
}
//非今天的日期
var fmt = " HH:mm"
if calendar.isDateInYesterday(self){
fmt = "昨天" + fmt
}else{
fmt = "MM-dd" + fmt
//直接获取 “年”的数值
let comps = calendar.dateComponents([.year], from: self, to: Date())
if let year = comps.year, year > 0 {
fmt = "yyyy-" + fmt
}
}
//根据格式字符串生成描述字符串
let df = DateFormatter()
df.dateFormat = fmt
df.locale = Locale(identifier: "en")
return df.string(from: self)
}
}2. 测试方法
override func viewDidLoad() {
super.viewDidLoad()
//目标: 特殊格式的字符串
//步骤
//1.建立 ‘日期’对象
//2.根据日期对象,计算显示描述字符
let str = "Mon Jul 25 16:41:43 +0800 2022"
print(Date.fromData(string: "Mon Jul 26 06:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 26 10:03:01 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 26 10:06:01 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 26 00:00:02 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 25 16:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 24 16:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon May 24 16:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon May 28 16:41:43 +0800 2021")?.dateDescription)
}3. 输出结果
Optional("3 小时前")
Optional("3 分钟前")
Optional("刚刚")
Optional("10 小时前")
Optional("昨天 16:41")
Optional("07-24 16:41")
Optional("05-24 16:41")
Optional("2021-05-28 16:41")边栏推荐
- Protective copy & stateless
- Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
- Method of converting inline elements to block elements
- DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
- Charles -- teach you how to use the packet capturing tool from 0-1
- 对接支付宝支付
- Five interesting magic commands in jupyter notebook
- Consumer unit 消费单元
- 将Word中的表格以图片形式复制到微信发送
- Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
猜你喜欢

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!

时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
![Error reporting: the network preview shows {xxx:['this field is required']}](/img/96/b0a6c01543fcbcc6d3262b3797fae2.jpg)
Error reporting: the network preview shows {xxx:['this field is required']}

Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology

iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器

Techo Hub 福州站干货来袭|与开发者共话工业智能新技术

状态压缩dp-蒙德里安的梦想

How to carry out engineering implementation of DDD Domain Driven Design

【AD学习】本次海上航行器大赛画pcb图的历程

Charles -- teach you how to use the packet capturing tool from 0-1
随机推荐
[Commons lang3 topic] 005- objectutils topic
如何给女友讲明白JS的bind模拟实现
ThinkPHP高仿蓝奏云网盘系统程序
可视化全链路日志追踪
AQS原理
How to explain JS' bind simulation implementation to your girlfriend
Seven marketing strategies of NFT project
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT
Tips for API interface optimization
【无标题】
Techo Hub 福州站干货来袭|与开发者共话工业智能新技术
大页内存原理及使用设置
[Commons lang3 topic] 001 stringutils topic
Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
Consumer unit
B+ 树 ~
【刷题笔记】二进制链表转整数
机器学习 | MATLAB实现RBF径向基神经网络newrbe参数设定
Asynchronous mode worker thread
新拟态个人引导页源码