iOS format specification

Source: Internet
Author: User

iOS format specification

Directory

    • Overview
    • Date format

Overview

Date format

Declaration Time format: NSDateFormatter *date_formatter = [[NSDateFormatter alloc] init];

Set value time format: [Date_formatter setdateformat:@ "yyyy mm month DD day hh" mm min ss seconds "];

Set value Locale:[date_formatter Setlocale:[[nslocale alloc] initwithlocaleindentifier:@ "en_US"] or [date_formatter Setlocale:[nslocale Currentlocale]];

Date obtained

Get current date: nsdate *current_date = [NSDate Date];

Date obtained from string: nsdate *custom_date = [Date_formatter stringfromdate:@ "20140905133212"];

Convert the acquisition date to a string in the appropriate format: nsstring *str = [Date_formatter stringfromdate:current_date];

NSDateFormatter Format Description

G: A.D., e.g. AD A.D.

YY: The latter 2 digits of the year

YYYY: Full year

MM: Month, display as 1-12

MMM: Month, abbreviated for the English month, such as Jan

MMMM: Month, display full name of the English month, such as January

DD: day, 2 digits, such as 2

D: Day, 1-2-bit display, such as 2

EEE: Abbreviated day of the week, such as Sun

EEEE: Full written day of the week, such as Sunday

AA: Last afternoon, AM/PM

H: When, 24-hour, 0-23

K: When, 12-hour, 0-11

M: minutes, 1-2-bit

MM: Min., 2-bit

S: seconds, 1-2 bits

SS: Seconds, 2 bits

S: MS

Common date Structure

YYYY-MM-DD HH:mm:ss.sss

YYYY_MM-DD HH:mm:ss

Yyyy-mm-dd

MM DD yyyy

Customize the displayed week format

When using the NSDateFormatter conversion date, the day of the week that gets the English alphabet can only be like this, such as sun, Mon, etc.

If you want the day of the week for capital letters, you can:

Nsarray*weekdayary = [Nsarray arraywithobjects:@ "SUN", @ "MON", @ "TUE", @ "WED", @ "THU", @ "FRI", @ "SAT", nil];

Dateformatter = [[NSDateFormatter alloc] init];

[Dateformatter setdateformat:nslocalizedstring (@ "YYYY. MM.dd.eee ", nil)];

Change the day of the week of the uppercase letters displayed here? [Dateformattersetshortweekdaysymbols:weekdayary];

[Dateformatter setlocale:[[nslocale alloc]initwithlocaleidentifier:@ "en_US"]];

Nstring *str= [dateformatter stringfromdate:[nsdate Date]];

Calculate distance How much time is there in a day

nsdate* toDate = [[NSDate alloc]initwithstring:@ "2012-9-29 0:0:00 +0600"];

nsdate* startdate = [[Nsdatealloc] init];

nscalendar* Chineseclendar = [[Nscalendar alloc]initwithcalendaridentifier:nsgregoriancalendar];

Nsuinteger unitflags =

Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit | Nsdaycalendarunit | Nsmonthcalendarunit | Nsyearcalendarunit;

Nsdatecomponents *cps = [Chineseclendar components:unitFlagsfromDate:startDate todate:todate options:0];

Nsinteger diffhour = [cps hour];

Nsinteger diffmin = [Cpsminute];

Nsinteger diffsec = [cps second];

Nsinteger diffday = [cps day];

Nsinteger Diffmon = [cps month];

Nsinteger diffyear = [cps year];

NSLog (@ "from-now-%@, diff:years:%d Months:%d, days;%d, Hours:%d, mins:%d,sec:%d",

[toDate description], diffyear, Diffmon, Diffday, diffhour,diffmin,diffsec);

iOS format specification

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.