An article thoroughly understand iOS, JS time and date (date, Calendar, Locale, TimeZone)

Source: Internet
Author: User
Tags time zones time and date


iOS time-related classes
    • NSDate-Represents an absolute point in time.
    • Nscalendar-Represents a specific calendar, such as the Gregorian or Hebrew calendar. It provides a series of date-based calculations and allows you to convert between "NSDate" and "nsdatecomponents" objects.
    • Nsdatecomponents-Allows you to get a specific content of date, such as hours, minutes, years, days, and so on.
    • Nstimezone-Represents a specific time zone information that can help compute tasks across time zones.
Code Analysis


Say less nonsense, Show me the Code


/**
     * Calendar
     */
    //Gregorian calendar
    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]);
    // Gregorian calendar: 2018-5-9
    
    //Buddhist calendar
    calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierBuddhist];
    NSLog (@ "% ld-% ld-% ld",
    [calendar component: NSCalendarUnitYear fromDate: date],
    [calendar component: NSCalendarUnitMonth fromDate: date],
    [calendar component: NSCalendarUnitDay fromDate: date]);
    // Buddhist calendar: 2561-5-9
    
    // Japanese calendar
    calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierJapanese];
    NSLog (@ "% ld-% ld-% ld",
    [calendar component: NSCalendarUnitYear fromDate: date],
    [calendar component: NSCalendarUnitMonth fromDate: date],
    [calendar component: NSCalendarUnitDay fromDate: date]);
    // Japanese calendar: 30-5-9
    
    /**
     * Region
     */
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle: NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle: NSDateFormatterLongStyle];
    NSString * formattedDateString = [dateFormatter stringFromDate: date];
    NSLog (@ "Default Locale Formatted Date:% @", formattedDateString);
    // The system is a Gregorian calendar: Default Locale Formatted Date: 9 May 2018 at 4:25:06 PM GMT + 8
    // The system is a Buddhist calendar: Default Locale Formatted Date: 9 May 2561 BE at 4:21:29 PM GMT + 8
    
    // Chinese Locale
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier: @ "zh_CN"];
    formattedDateString = [dateFormatter stringFromDate: date];
    NSLog (@ "ZH Locale Formatted Date:% @", formattedDateString);
    // ZH Locale Formatted Date: May 9, 2018 GMT + 8 PM 4:21:29 PM
    
    /**
     * Time zone
     */
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName: @ "GMT"];
    formattedDateString = [dateFormatter stringFromDate: date];
    NSLog (@ "GMT Timezone Formatted Date:% @", formattedDateString);
    // GMT Timezone Formatted Date: May 9, 2018 GMT 8:21:29 AM
    
    /**
     * 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: November 6, 1st GMT 3:54:17 PM 
Javascript


A Date object about JavaScript can refer to the following links:
Understanding Date and Time in JavaScript
JavaScript Date Objects


Get Date Property
 
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 formatting
 
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 is a very powerful JavaScript time-and-date library that is a good extension of native objects.


 
//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


There is also a newer, more modern time-date library: Luxon, you can try it.



An article thoroughly understand iOS, JS time and date (date, Calendar, Locale, TimeZone)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.