iOS

时间相关类

  • NSDate - 表示一个绝对的时间点。
  • NSCalendar - 代表一个特定的日历,例如公历或者希伯来日历。它提供了一系列基于日期的计算,并且可以让你在"NSDate"和"NSDateComponents"对象之间进行转换。
  • NSDateComponents - 允许你获取一个Date的特定内容,例如小时、分钟、年月日等等。
  • NSTimeZone - 代表一个特定的时区信息,可以帮助跨时区的计算任务。

代码分析

废话少说,Show me the code

    /**
* 日历
*/
//公历
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *date = [NSDate new];
NSLog(@"%ld-%ld-%ld",
[calendar component:NSCalendarUnitYear fromDate:date],
[calendar component:NSCalendarUnitMonth fromDate:date],
[calendar component:NSCalendarUnitDay fromDate:date]);
// 公历:2018-5-9 //佛历
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierBuddhist];
NSLog(@"%ld-%ld-%ld",
[calendar component:NSCalendarUnitYear fromDate:date],
[calendar component:NSCalendarUnitMonth fromDate:date],
[calendar component:NSCalendarUnitDay fromDate:date]);
// 佛历:2561-5-9 //日本日历
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese];
NSLog(@"%ld-%ld-%ld",
[calendar component:NSCalendarUnitYear fromDate:date],
[calendar component:NSCalendarUnitMonth fromDate:date],
[calendar component:NSCalendarUnitDay fromDate:date]);
// 日本日历:30-5-9 /**
* 地区
*/
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"Default Locale Formatted Date:%@", formattedDateString);
// 系统为公历:Default Locale Formatted Date:9 May 2018 at 4:25:06 PM GMT+8
// 系统为佛历:Default Locale Formatted Date:9 May 2561 BE at 4:21:29 PM GMT+8 //中国Locale
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"ZH Locale Formatted Date:%@", formattedDateString);
// ZH Locale Formatted Date:2018年5月9日 GMT+8 下午4:21:29 /**
* 时区
*/
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"GMT Timezone Formatted Date:%@", formattedDateString);
// GMT Timezone Formatted Date:2018年5月9日 GMT 上午8:21:29 /**
* NSDateComponents
*/
// Yearless date
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMonth:11];
[components setDay:7];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *birthday = [gregorian dateFromComponents:components];
formattedDateString = [dateFormatter stringFromDate:birthday];
NSLog(@"GMT Timezone Formatted Date:%@", formattedDateString);
// GMT Timezone Formatted Date:1年11月6日 GMT 下午3:54:17

JavaScript

关于JavaScript的Date对象可以参考以下链接:
Understanding Date and Time in JavaScript
JavaScript Date Objects

获取Date属性

const birthday = new Date(1980, 6, 31);
birthday.getFullYear(); // 1980
birthday.getMonth(); // 6
birthday.getDate(); // 31
birthday.getDay(); // 4
birthday.getHours(); // 0
birthday.getMinutes(); // 0
birthday.getSeconds(); // 0
birthday.getMilliseconds(); // 0
birthday.getTime(); // 333849600000 (for GMT)

Date格式化

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today = new Date(); today.toLocaleDateString("en-US"); // 5/9/2018
today.toLocaleDateString("en-US",options); // Wednesday, May 9, 2018
today.toLocaleDateString("hi-IN", options); // बुधवार, 9 मई 2018

Moment.js

Moment.js -Parse, validate, manipulate, and display dates and times in JavaScript。Moment是一个非常强大的JavaScript时间日期库,是对原生对象的很好的扩展。

//Format
moment().format('MMMM Do YYYY, h:mm:ss a'); // May 9th 2018, 8:05:15 pm //Calendar Time
moment().add(10, 'days').calendar(); //Multiple Locale Support
moment.locale(); // en //TimeZone
var jun = moment("2014-06-01T12:00:00Z");
var dec = moment("2014-12-01T12:00:00Z"); jun.tz('America/Los_Angeles').format('ha z'); // 5am PDT
dec.tz('America/Los_Angeles').format('ha z'); // 4am PST

目前还有一个更新、更现代的时间日期库:luxon,可以尝试一下。

一篇文章吃透iOS、JS的时间日期(Date, Calendar, Locale, TimeZone)的更多相关文章

  1. 一篇文章看懂JS闭包,都要2020年了,你怎么能还不懂闭包?

     壹 ❀ 引 我觉得每一位JavaScript工作者都无法避免与闭包打交道,就算在实际开发中不使用但面试中被问及也是常态了.就我而言对于闭包的理解仅止步于一些概念,看到相关代码我知道这是个闭包,但闭包 ...

  2. js 格式化时间日期函数小结

    下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下. 代码如下: Date.prototype.format = function(format){ var o = { &q ...

  3. 修改上一篇文章的node.js代码,支持调用自定义页面

    上一篇文章所有请求只能调用index.html,现在做个改造,允许调用自定义页面 服务端 app.js var app = require('http').createServer(handler) ...

  4. 001 -js对时间日期的排序

    001-JS对时间日期的排序 最近在做公司的项目时间,产品给了一个很简单的页面,让帮忙写一下.首先看一下产品的需求: 需要对该列表进行排序 思路:(1)可以在数据库写sql语句的时间直接一个DESC按 ...

  5. iOS - 获取当前时间日期星期几

    //获取当前时间日期星期 - (NSString *)getCurrentTimeAndWeekDay { NSArray * arrWeek=[NSArray arrayWithObjects:@& ...

  6. js实现时间日期的格式化

    前几天参加cvte的笔试,碰到了这样一道题目: //请写一个时间日期格式化的函数,具体要求如下: function format(date,"yyyy-mm-dd HH-mm-ss" ...

  7. js 格式化时间日期函数小结3

    function DateUtil(){}/***功能:格式化时间*示例:DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 ...

  8. js 获取时间 new Date()详细介绍

    javaScript系列:js中获取时间new Date()详细介绍 (2012-03-31 09:54:25) 转载▼ 标签: js时间 new date() 字符类型 转换 分类: study-j ...

  9. linux 关于时间日期date

    一.查看和修改Linux的时区 1. 查看当前时区 命令 : "date -R" 2. 修改设置Linux服务器时区 方法 A 命令 : "tzselect" ...

随机推荐

  1. [Bayes] *Bayesian Deep Learning for Transparency Improvement

    为何有必要进修统计机器学习? 因为你没有那么多的数据 因为未知的东西最终还是需理论所解释 基于规则?基于概率? ---- 图灵奖得主.贝叶斯之父 Judea Pearl 谈深度学习局限,想造自由意志机 ...

  2. Extjs5 app.js缓冲设置

    在6月2日Extjs5正式版公布后.粗略研究了一下,sencha推荐使用project编译来公布应用.开发过程中用sencha app watch命令就可以生成服务.每建立一个js类,就须要Ctrl+ ...

  3. jdk从1.8降到1.7的办法

    jdk从1.8降到1.7的办法 正常情况下,JDK的升降级只需更改环境变量即可,可是到了1.8版本,oracle使了一点小手段--在系统Path中添加一条变量C:/ProgramData/Oracle ...

  4. TortoiseGit上传项目代码到github方法(超简单)

    Github是咱广大开发者用的非常多的项目代码版本管理网站,项目托管可以是私人的(private)或者公开的(public),私人的收费,一个月7美金.咱这里就只说我们个人使用的,一般都是代码对外开放 ...

  5. Linux的文件权限(简单易懂)

    学习这个章节,必须明白以下三个概念: 1.所有者 2.所属组 3.其他人 明白这三个概念后,接下来就学习文件的属性,那么文件的属性有什么呢?如何查看文件的属性? 在命令行下,执行 ls -l 可以得到 ...

  6. 微信 openId

    摘要 关于微信开发的话题,例子确实已经有不少,但大部分都是人云亦云,很多小细节或者需要注意的地方却大多没有讲清楚,这令很多刚开始开发的人感觉大很迷茫.而我今天要说的话题,主要着眼于两个方面. 一:如何 ...

  7. 蓝桥杯 第三届C/C++预赛真题(2) 古堡算式(数学题)

    福尔摩斯到某古堡探险,看到门上写着一个奇怪的算式: ABCDE * ? = EDCBA 他对华生说:“ABCDE应该代表不同的数字,问号也代表某个数字!” 华生:“我猜也是!” 于是,两人沉默了好久, ...

  8. ChemDraw进行自动调整的步骤

    说到化学绘图软件那就不得不提ChemDraw,起非常的经典在国内外都得到了普遍应用,最新版是ChemDraw 15.1 Pro.在使用ChemDraw化学绘图工具绘制化学图形的时候,需要循序渐进一步一 ...

  9. 面试之JavaWeb篇(十三)

    148,AJAX有哪些有点和缺点? 优点: 1.最大的一点是页面无刷新,用户的体验非常好. 2.使用异步方式与服务器通信,具有更加迅速的响应能力. 3.可以把以前一些服务器负担的工作转嫁到客户端,利用 ...

  10. [Go语言]从Docker源码学习Go——main函数

    Go程序从main包下的main函数开始执行,当main执行结束后,程序退出. Docker的main函数在 docker/docker/docker.go package main //Import ...