这看起来很奇怪……我在 actionSheet 中使用了 UidatePicker。单击一个按钮,我会显示操作表。当我选择日期为 2012 年 12 月 31 日并单击完成时,我得到的日期是 2013 年 12 月 31 日。它发生在所有年份,仅在 12 月 31 日发生。我做错了什么吗?这是我的代码
-(void)dateButtonPressed:(id)sender //Method invoking the action sheet on button press
{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
closingdatePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
closingdatePicker.tag=10;
closingdatePicker.datePickerMode=UIDatePickerModeDate;
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(DatePickerCancelClick:)]; …Run Code Online (Sandbox Code Playgroud) 我想按日期格式化程序生成日期,
2012-07-12但它显示为
2012-07-11
我的代码:
NString * today_selected=[NSString stringWithFormat:@"%d%@%d%@%d",year_for_activated,@"-",month_for_activated,@"-",taged]; NSDateFormatter *Df=[[NSDateFormatter alloc]init];
Run Code Online (Sandbox Code Playgroud)
// here year_of_ = 2012和month_of_ac = 7,tag = 12
但它显示2012-07-11而不是12.
[Df setDateFormat:@"YYYY-MM-DD" ];
NSDate *date_selected=[Df dateFromString: today_selected];
NSLog(@"today_selected:%@",date_selected);
Run Code Online (Sandbox Code Playgroud)
但它显示2012-01-12
我怎样才能获得像“11:30”这样的时间,以便我想将它与以下内容进行比较:
strOpenTime = @"10:00";
strCloseTime = @"2:00";
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能获得像上面打开/关闭时间格式一样的当前时间,我想要当前时间是否在打开/关闭时间间隔内?
提前致谢..!!
我正在尝试将日历(Kal 日历实现)上选择的日期与当前日期进行比较。问题是当前日期采用 MM/dd/yyyy 格式,而如果月份或日期低于 10,则 Kal 日期将没有前导零。是否有一种简单的方法来检索当前日期,以便它不会有如果天数或月数小于 10,则前导零?(当前日期实际上将用作保存对象的属性,以便以后可以使用 Kal 选择的日期查询它们)
示例当前日期 - 07/07/2014
示例 Kal 日期 - 7/7/2014
嗨我有一个问题场景与日期比较与给定的日期范围,场景如下:
我有一个包含数据的数组:
var filter1Date = [String]()
filter1Date[Wed, 06 May 2015 03:44:19 GMT, Wed, 06 May 2015 03:36:27 GMT, Wed, 06 May 2015 02:56:51 GMT, Wed, 06 May 2015 01:54:25 GMT, Tue, 05 May 2015 19:17:18 GMT, Wed, 06 May 2015 02:57:59 GMT, Wed, 06 May 2015 02:07:38 GMT, Wed, 06 May 2015 01:53:14 GMT, Tue, 05 May 2015 14:30:10 GMT, Tue, 05 May 2015 14:04:34 GMT]
Run Code Online (Sandbox Code Playgroud)
现在我有两个日期这给从和到两个日期例如:
var fromDate = "dd-MM-yyyy"
var toDate = "dd-MM-yyyy"
Run Code Online (Sandbox Code Playgroud)
现在我想比较 …
我的应用程序中有一个 NSTimer 对象,它以秒为单位计算经过的时间。
我想以符合众所周知的标准的方式在我的应用程序界面中格式化 UILabel。
例子
00:01 - 一秒
01:00 - 60 秒
01:50:50 - 6650 秒
我想知道如何做到这一点,您知道任何基于 Int 秒数创建此类 String 的豆荚/库吗?
显然我自己可以使用复杂的方法,但由于建议不要重新发明轮子,我更愿意使用一些现成的解决方案。
我在 Foundation 库和 HealthKit 中都没有找到任何相关内容
你对如何完成它有什么建议吗?如果你说“自己去写吧”——那没问题。但我想确保我没有遗漏任何简单、直接的解决方案。
提前致谢
我正在尝试获得类似于 facebook 发布日期/时间的内容。像 1 分钟、1 小时、昨天、一周前等。
这是我用来获取这种格式的函数:( Oct-22 17:28 PM)
func convertTime(serverTime: Double) -> String {
let serverTime = serverTime
let date = Date(timeIntervalSince1970: serverTime)
let dateFomatter = DateFormatter()
dateFomatter.dateFormat = "MMM-dd HH:mm a"
let strDate = dateFomatter.string(from: date)
print("This is the post's date: \(strDate)")
return strDate
}
Run Code Online (Sandbox Code Playgroud) 我想检查两个日期是否相等,其中相等意味着它们与分钟的值相同.我不想比较秒值.
例如:
// should be equal
11/12/17 11:19:29
11/12/17 11:19:03
// not equal
11/12/17 11:19:59
11/12/17 11:20:00
Run Code Online (Sandbox Code Playgroud)
我正在考虑这样做的一种方法是获取时间组件并通过每个组件.但我想知道是否有更快或更清洁的方式.
我试图在这里做一些非常简单的事情,但失败了。我只想将带有完整日期的字符串转换为小时和分钟,有人能看到我哪里出错了吗?以下代码打印 nil
let dateString2 = "2018-03-11 20:43:05 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let dateObj = dateFormatter.date(from: dateString2)
dateFormatter.dateFormat = "hh:mm"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
Run Code Online (Sandbox Code Playgroud) 我有两个日期,即2/02/2016和19/03/2018
我正在尝试获取这些日期之间的月份和年份
输出应该是
2016 年 2 月、2016 年 3 月……等等……2018 年 1 月、2018 年 2 月、2018 年 3 月。
试过月间隙代码 -
let date1 = DateComponents(calendar: .current, year: 2016, month: 2, day: 2).date!
let date2 = DateComponents(calendar: .current, year: 2018, month: 3, day: 19).date!
let monthGap = Calendar.current.dateComponents([.month], from: date1, to: date2)
print("monthGap is \(String(describing: monthGap.month))")//prints monthGap is 25
Run Code Online (Sandbox Code Playgroud) nsdate ×10
swift ×5
ios ×4
cocoa-touch ×3
xcode6 ×2
compare ×1
date ×1
ios5 ×1
iphone ×1
kal ×1
nscalendar ×1
nstimer ×1
objective-c ×1
swift3 ×1
swift4 ×1
uidatepicker ×1