【问题标题】:How to get first date of MONTH, and first date of YEAR for a incoming date. in iPhone Objective c [duplicate]如何获取 MONTH 的第一个日期,以及传入日期的 YEAR 的第一个日期。在 iPhone 目标 c [重复]
【发布时间】:2013-01-11 07:23:02
【问题描述】:

可能重复:
How to get all the days/dates in current week, this month, this year using the current date (today) in iPhone

我需要获取月份的第一天,日期的第一天

(大多数时候输入日期是当前日期[NSDate date]

能否请教一下如何获取呢,

例如今天当前日期 = 3-MAY-2015

需要的输出如下:

First_Date_month = 1-MAY-2015

First_Date_year = 1-JAN-2015

提前感谢您的宝贵时间

【问题讨论】:

  • 只有本论坛才有的素材很多,多搜几遍吧。您也可以先告诉我们您是如何设置日期格式的以及什么不起作用。
  • 这似乎本质上是stackoverflow.com/questions/14272489/… 的转贴。请不要那样做。

标签: iphone objective-c nsdate nscalendar nsdatecomponents


【解决方案1】:

您可以使用 NSDateComponents 找到日期月份和年份,并且可以根据需要制作 NSDate

NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
[components setDay:1];
self.currentDate = [calendar dateFromComponents:components];
int m = components.month;
int y = components.year;
int d = components.day;


NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy MM dd"];
NSDate *firstDateOfMonth = [df dateFromString:[NSString stringWithFormat:@"%@ %@ 01",y,m]];
NSDate *firstDateOfYear = [df dateFromString:[NSString stringWithFormat:@"%@ 01 01",y]];

【讨论】:

  • 谢谢你它工作正常,但它有时间 GMT +5.30(我的所在地是印度)任何解决方案
  • google it .. NSDateFormatter 会帮助你
猜你喜欢
  • 2013-02-09
  • 2012-05-29
  • 1970-01-01
  • 2010-12-12
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多