Simple usage of nsdate

Source: Internet
Author: User
Tags date1

NSDate the current time obtained using the date method is 0 time zone

NSDate *date = [NSDate Date];

NSLog (@ "%@", date);

If you want to get the current time zone, you need + 8 hours

NSDate *now = [nsdate datewithtimeintervalsincenow:8 * 60 * 60];

NSLog (@ "%@", now);

Get yesterday at this time

NSDate *yesterday = [nsdate datewithtimeintervalsincenow:8 * 60 * 60-24 * 60 * 60];

NSLog (@ "%@", yesterday);

Get the time tomorrow

NSDate *tomorrow = [[NSDate alloc] Initwithtimeintervalsincenow:8 * 60 * 60 + 24 * 60 *60];

NSLog (@ "%@", Tomorrow);

Get a two-time interval

Timeintervalsincedate: Gets the time interval unit for two time objects: seconds

Nstimeinterval interval = [tomorrow Timeintervalsincedate:yesterday];

NSLog (@ "%.2f", INTERVAL/60/60);

Simulate a simple chat

Receive information

NSLog (@ "Hello!");

Record the time the message was received

NSDate *getmessage = [NSDate Date];

Send Message

1. Input string from keyboard

Char ch[100] = {0};

scanf ("%s", ch);

2. Convert C-language strings to OC strings

NSString *message = [NSString stringwithutf8string:ch];

Record when a message is sent

NSDate *sendmessage = [NSDate Date];

Time interval to get two messages

Nstimeinterval interval1 = [SendMessage timeintervalsincedate:getmessage];

if (interval1< 60) {

NSLog (@ "just..");

}else if (Interval1 > 60&& interval< 60 * 60) {

NSLog (@ "%.f minutes ago", INTERVAL1/60);

}

NSLog (@ "%@", message);

Get current time

NSDate *date1 = [NSDate Date];

NSDateFormatter date format class to control date conversion format '

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

Setting the time zone

[Nstimezone Localtimezone] Get your local time zone

[Formatter Settimezone:[nstimezone Localtimezone];

Set date formatting style

[Formatter Setdatestyle:nsdateformattermediumstyle];

Set the time style

[Formatter Settimestyle: (Nsdateformatterfullstyle)];

Convert NSDate to NSString by creating a date format

NSString *datestr = [Formatter stringfromdate:date1];

NSLog (@ "%@", datestr);

Custom Date format Classes

Format Class object

NSDateFormatter *myformatter = [[NSDateFormatter alloc] init];

Setting the time zone

[MyFormatter Settimezone:[nstimezone Localtimezone];

Custom Date-time formats

YYYY represents the year mm representing the month DD Representative Day

HH (HH) on behalf of the hour 24-hour system (12-hour system)

Eeee Week

[MyFormatter setdateformat:@ "Yyyy-mm-dd hh:mm:ss eeee oooo"];

Transformation

NSString *datestr1 = [MyFormatter stringfromdate:date];

NSLog (@ "%@", DATESTR1);

Convert a date string to a Date object

Set the date format string must be identical to the date string

@ "January 30, 2015 11:47"

1. Create a Date Format class object

NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];

2. Set the time zone

[Formatter1 Settimezone:[nstimezone Localtimezone];

Set Date Time format

[Formatter1 setdateformat:@ "yyyy mm month DD day hh:mm"];

4. Conversions

NSDate *date2 = [Formatter datefromstring:@ "January 30, 2015 11:47"];

NSLog (@ "%@", date2);

Converts the string @ "2015013102318" to a NSDate object

1. Create a Date Format class object

NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];

2. Formatting

[Formatter2 Settimezone:[nstimezone Localtimezone];

[Formatter2 setdateformat:@ "YYYYMMDDHHMMSS"];

3. Conversions

NSDate *date3 = [Formatter datefromstring:@ "20150131023108"];

NSLog (@ "%@", date3);

}

Simple usage of nsdate

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.