IOS------Get current time and current timestamp

Source: Internet
Author: User

Gets the current time

+ (nsstring*) getcurrenttimes{

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

----------set the format you want, the difference between HH and HH: 12-hour, 24-hour system, respectively.

[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];

Now time, you can output to see what the format is

NSDate *datenow = [NSDate Date];

----------convert NSDate to NSString by formatter format

NSString *currenttimestring = [Formatter stringfromdate:datenow];

NSLog (@ "currenttimestring =%@", currenttimestring);

return currenttimestring;

}

There are two ways to get the current timestamp (in seconds)

+ (NSString *) getnowtimetimestamp{

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

[Formatter Setdatestyle:nsdateformattermediumstyle];

[Formatter Settimestyle:nsdateformattershortstyle];

[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; ----------set the format you want, the difference between HH and HH: 12-hour, 24-hour system, respectively.

Setting the time zone, which is sometimes important

nstimezone* TimeZone = [Nstimezone timezonewithname:@ "Asia/shanghai"];

[Formatter Settimezone:timezone];

NSDate *datenow = [NSDate date];//now time, you can output to see what format

NSString *timesp = [NSString stringwithformat:@ "%ld", (long) [Datenow timeIntervalSince1970]];

return TIMESP;

}

+ (NSString *) getnowtimetimestamp2{

nsdate* dat = [nsdate datewithtimeintervalsincenow:0];

Nstimeinterval A=[dat timeIntervalSince1970];

nsstring*timestring = [NSString stringwithformat:@ "%0.f", a];//to character type

;

return timestring;

}

Gets the current timestamp (in milliseconds)

+ (NSString *) getnowtimetimestamp3{

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

[Formatter Setdatestyle:nsdateformattermediumstyle];

[Formatter Settimestyle:nsdateformattershortstyle];

[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss SSS"]; ----------set the format you want, the difference between HH and HH: 12-hour, 24-hour system, respectively.

Setting the time zone, which is sometimes important

nstimezone* TimeZone = [Nstimezone timezonewithname:@ "Asia/shanghai"];

[Formatter Settimezone:timezone];

NSDate *datenow = [NSDate date];//now time, you can output to see what format

NSString *timesp = [NSString stringwithformat:@ "%ld", (long) [Datenow timeintervalsince1970]*1000];

return TIMESP;

}

IOS------Get current time and current timestamp

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.