NSDateFormatter的格式字符串

本文介绍了如何使用NSDateFormatter来格式化日期和时间,并提供了详细的代码示例。通过设置不同的日期和时间样式,可以灵活地实现多种格式的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  1.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
  2.     [dateFormatter setDateStyle:NSDateFormatterMediumStyle];  
  3.     [dateFormatter setTimeStyle:NSDateFormatterShortStyle];  
  4.     //[dateFormatter setDateFormat:@"hh:mm:ss"]   
  5.     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS" ];       //毫秒   
  6.     NSLog(@"Date%@" , [dateFormatter stringFromDate:[NSDate date]]);  
  7.     [dateFormatter release]; 

 

 

Specifier

Description

%%

A '%' character

%a

Abbreviated weekday name

%A

Full weekday name

%b

Abbreviated month name

%B

Full month name

%c

Shorthand for “%X %x ", the locale format for date and time

%d

Day of the month as a decimal number (01-31)

%e

Same as %d but does not print the leading 0 for days 1 through 9 (unlike strftime() , does not print a leading space)

%F

Milliseconds as a decimal number (000-999)

%H

Hour based on a 24-hour clock as a decimal number (00-23)

%I

Hour based on a 12-hour clock as a decimal number (01-12)

%j

Day of the year as a decimal number (001-366)

%m

Month as a decimal number (01-12)

%M

Minute as a decimal number (00-59)

%p

AM/PM designation for the locale

%S

Second as a decimal number (00-59)

%w

Weekday as a decimal number (0-6), where Sunday is 0

%x

Date using the date representation for the locale, including the time zone (produces different results from strftime() )

%X

Time using the time representation for the locale (produces different results from strftime() )

%y

Year without century (00-99)

%Y

Year with century (such as 1990)

%Z

Time zone name (such as Pacific Daylight Time; produces different results from strftime() )

%z

Time zone offset in hours and minutes from GMT (HHMM)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值