IOS ------ get the current time and current timestamp, ios ------ current time

Source: Internet
Author: User

IOS ------ get the current time and current timestamp, ios ------ current time

// Obtain the current time

+ (NSString *) getCurrentTimes {

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

// ---------- Set the format you want. The difference between hh and HH: 12-hour and 24-hour respectively.

[Formatter setDateFormat: @ "YYYY-MM-dd HH: mm: ss"];

// The current time. You can output the following format:

NSDate * datenow = [NSDate date];

// ---------- Convert nsdate to nsstring in formatter format

NSString * currentTimeString = [formatter stringFromDate: datenow];

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

Return currentTimeString;

}

You can obtain the current timestamp in seconds)

+ (NSString *) getNowTimeTimestamp {

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

[Formatter setDateStyle: NSDateFormatterMediumStyle];

[Formatter setTimeStyle: nsdateformatter1_style];

[Formatter setDateFormat: @ "YYYY-MM-dd HH: mm: ss"]; // ---------- set the format you want. The difference between hh and HH is 12-hour, respectively, 24-hour system

// Set the time zone, which is sometimes important for Time Processing

NSTimeZone * timeZone = [NSTimeZone timeZoneWithName: @ "Asia/Shanghai"];

[Formatter setTimeZone: timeZone];

NSDate * datenow = [NSDate date]; // The current time. You can output the following 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]; // convert it to struct

;

Return timeString;

}

// Obtain the current timestamp (in milliseconds)

+ (NSString *) getNowTimeTimestamp3 {

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

[Formatter setDateStyle: NSDateFormatterMediumStyle];

[Formatter setTimeStyle: nsdateformatter1_style];

[Formatter setDateFormat: @ "YYYY-MM-dd HH: mm: ss SSS"]; // ---------- set the format you want. The difference between hh and HH is as follows: 12-hour and 24-hour respectively

// Set the time zone, which is sometimes important for Time Processing

NSTimeZone * timeZone = [NSTimeZone timeZoneWithName: @ "Asia/Shanghai"];

[Formatter setTimeZone: timeZone];

NSDate * datenow = [NSDate date]; // The current time. You can output the following format:

NSString * timeSp = [NSString stringWithFormat: @ "% ld", (long) [datenow timeIntervalSince1970] * 1000];

Return timeSp;

}

 

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.