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

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,可以尝试一下。

原文地址:https://www.cnblogs.com/wdsunny/p/9026802.html

时间: 2024-08-03 15:54:21

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

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

下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下. 代码如下: Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(

js中时间日期的处理

// 增加天 function AddDays(date,value)  {     date.setDate(date.getDate()+value);  }   // 增加月 function AddMonths(date,value)  {     date.setMonth(date.getMonth()+value);  }   // 增加年 function AddYears(date,value)  {     date.setFullYear(date.getFullYear(

一篇文章看懂JS执行上下文

 壹 ? 引 我们都知道,JS代码的执行顺序总是与代码先后顺序有所差异,当先抛开异步问题你会发现就算是同步代码,它的执行也与你的预期不一致,比如: function f1() { console.log('听风是风'); }; f1(); //echo function f1() { console.log('echo'); }; f1(); //echo 按照代码书写顺序,应该先输出 听风是风,再输出 echo才对,很遗憾,两次输出均为 echo:如果我们将上述代码中的函数声明改为函数表达式,

js 格式化时间日期

Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds()

js中时间new Date()详解以及实例

介绍 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-????) myDate.getMonth(); //获取当前月份(0-11,0代表1月) // 所以获取当前月份是myDate.getMonth()+1; myDate.getDate(); //获取当前日(1-31) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) m

js关于时间(date)的比较

之前在工作上遇到一个问题:使用一些时间插件,如果有俩个时间,要判断结束时间和开始时间的大小?后来就查找了一些资料,这边整理出俩个比较简便的方法. 在这我拿 laydate.js 这个插件来举例: 首先是 html 代码,我简单的写了俩个时间框: 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name

时间日期Date类型

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> /*push和pop为堆栈后进先出LIFO,unshift和shift为队列先进先出FILO*/ var colors = [&quo

Js获取当前日期时间+日期印证+判断闰年+日期的天数差+日期格式化+JS判断某年某月有多少天

Js获取当前日期时间+日期验证+判断闰年+日期的天数差+日期格式化+JS判断某年某月有多少天 字符串转日期型+Js当前日期时间+日期验证+判断闰年+日期的天数差+日期格式化+日期所在年的第几周 日期时间脚本库方法列表Date.prototype.isLeapYear 判断闰年Date.prototype.Format 日期格式化Date.prototype.DateAdd 日期计算Date.prototype.DateDiff 比较日期差Date.prototype.toString 日期转字符

js获取实时日期和时间

//在相应位置显示时间日期 <div style=" width:400px; height:50px">    <span id="showtime"></span>     <!--走马灯文字 <marquee direction="left">需要滚动的内容</marquee>-->    </div></body> js获取时间日期 <s