当前位置:网站首页>Date conversion EEE MMM DD hh:mm:ss zzz YYYY
Date conversion EEE MMM DD hh:mm:ss zzz YYYY
2022-07-29 01:09:00 【Hanyang Li】
1. Create date conversion classification
//
// Date+Extension.swift
//
import Foundation
extension Date{
/// Convert a formatted string to a 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)
}
/**
Return the multi description information of the current date
just ( In a minute )
x Minutes ago ( In an hour )
x Hours before ( same day )
yesterday HH:mm( yesterday )
MM-dd HH:mm( Within a year )
yyyy-MM-dd HH:mm( Earlier )
*/
var dateDescription: String{
// Take out the current calendar - Provides a large number of calendar related operation functions
let calendar = Calendar.current
// Process today's date
if calendar.isDateInToday(self) {
let delta = Int(Date().timeIntervalSince(self))
if delta < 60 {
return " just "
}
if delta < 3600{
return "\(delta/60) Minutes ago "
}
return "\(delta/3600) Hours before "
}
// Date not today
var fmt = " HH:mm"
if calendar.isDateInYesterday(self){
fmt = " yesterday " + fmt
}else{
fmt = "MM-dd" + fmt
// Direct access to “ year ” The numerical
let comps = calendar.dateComponents([.year], from: self, to: Date())
if let year = comps.year, year > 0 {
fmt = "yyyy-" + fmt
}
}
// Generate a description string according to the format string
let df = DateFormatter()
df.dateFormat = fmt
df.locale = Locale(identifier: "en")
return df.string(from: self)
}
}2. The test method
override func viewDidLoad() {
super.viewDidLoad()
// The goal is : Special format string
// step
//1. establish ‘ date ’ object
//2. According to the date object , Calculate and display description characters
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. Output results
Optional("3 Hours before ")
Optional("3 Minutes ago ")
Optional(" just ")
Optional("10 Hours before ")
Optional(" yesterday 16:41")
Optional("07-24 16:41")
Optional("05-24 16:41")
Optional("2021-05-28 16:41")边栏推荐
- Definition of double linked list~
- Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
- 深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- QT static compiler (MinGW compilation)
- SQL server only has database files and no log files. The solution to the 1813 error in restoring data times
- State compression DP Mondrian's dream
- Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
- Charles -- teach you how to use the packet capturing tool from 0-1
- DDD领域驱动设计如何进行工程化落地
猜你喜欢

Solid smart contract tutorial (5) -nft auction contract

Plato launched the LAAS protocol elephant swap, which allows users to earn premium income

PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益

Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network

18张图,直观理解神经网络、流形和拓扑

State compression DP Mondrian's dream

🧐 table1 | 一秒搞定你的三线表

Cookie和Session
![[raspberry pie] how does the windows computer connect with raspberry pie](/img/d6/42685bbc4e4af757867442b63ce9c8.png)
[raspberry pie] how does the windows computer connect with raspberry pie

一文让你搞懂MYSQL底层原理。-内部结构、索引、锁、集群
随机推荐
双链表的定义 ~
电子招标初学者指南
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
进程和线程知识点总结 2
How to explain JS' bind simulation implementation to your girlfriend
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板
[Commons lang3 topic] 003- randomstringutils topic
NFT 项目的 7 种市场营销策略
【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
自制 | 纯手工自制一个16位RISC架构CPU
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT
system verilog常用语法
【刷题笔记】链表内指定区间反转
SystemVerilog join and copy operators
Wechat campus bathroom reservation applet graduation design finished product (5) assignment
Daniel guild Games: summary and future outlook of this year
Kwai focuses on regulating the number maintenance behavior in the ways of handling and manuscript washing, and how to purify the content ecology on the we media platform
指令重排、happens-before、as-if-serial
AQS principle
Day2:三种语言暴刷牛客130题