程序员

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

2018-05-09  本文已影响0人  奶爸码农

iOS

时间相关类

代码分析

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

上一篇下一篇

猜你喜欢

热点阅读