检查当前日期字符串是否在两个其他日期的范围之内或之外 [英] Check if the current date string is within or outside the range of two other dates

查看:137
本文介绍了检查当前日期字符串是否在两个其他日期的范围之内或之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个日期选择器。一个是选择一个开始日期,另一个是选择一个到期日。当我进行选择时,我将日期转换为字符串格式并将结果存储在两个文本字段中。



这是我如何保存到数据库:

  let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat =MMMM d// eg 9月15日
让sDate = dateFormatter.dateFromString(startDateField.text)为NSDate!
让eDate = dateFormatter.dateFromString(expiryDateField.text)为NSDate!

activity [startDate] = sDate
activity [expiryDate] = eDate

activity.saveInBackgroundWithBlock({(success:Bool,error:NSError! ) - > Void in

我打算做的是有一个表视图显示2个部分:一个显示活动的活动,另一个显示已过期/不在日期范围内的活动。



我想我可以通过获取当前日期并检查它在开始日期和到期日期范围内。



我从上面保存的日期显示在这样的解析中:





事情是我不想考虑年龄或时间,唯一重要的是月和日,所以例如一个活动可能在9月15日至12月20日之间每年运行一次。



我需要一种方法来检查当前日期是否在这两个d内吃了一天(例如, 15日)。所以今天的日期是10月19日,如果开始日期是9月15日和12月20日的有效期,活动将在范围内,但是如果到期日为10月18日或开始日期为10月25日,则为超出范围/操作。 / p>

总结:



我将在表视图中显示两个部分。一个活跃的活动,另一个是非活跃的失效活动。


  1. 如何将我的存储开始日期和到期日期并且容易检查当前日期是否在其范围内?


  2. 对于用户选择开始日期或到期日期的情况,我需要检查当前日期大于/小于开始日期(能够决定活动是否应该出现),或者如果当前日期大于/小于到期日期(能够决定活动是否通过或不通过)它的到期日期。


如果我可以将1和2卷到一个函数中,并在表视图。



有兴趣了解更多有经验的程序员将如何实现这一点。

解决方案

而不是日期格式MMMM d(例如9月15日),您可以将日期
转换为日期格式为MMdd的字符串(例如 0915),然后您可以在当前日期,开始日期和到期日期之间进行简单的
字符串比较。



例如,

 0915< 1019< 1220

所以10月19日的范围是从9月15日到12月20日。


I have 2 date pickers. One is for selecting a start date and the other is for selecting an expiry date. When a selection has been made I convert the date to string format and store the results in a two text fields.

This is how I save to the database:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d" // e.g. September 15
let sDate = dateFormatter.dateFromString(startDateField.text) as NSDate! 
let eDate = dateFormatter.dateFromString(expiryDateField.text) as NSDate!

activity["startDate"] = sDate
activity["expiryDate"] = eDate

activity.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in

What I intend to do is have a table view display 2 sections: one showing activities that are active, and the other showing activities that are expired/not in date range.

I figured I could do this by taking the current date and checking it was within the start date and expiry date range.

My saved date from above is showing up in parse like this:

The thing is, I don't want to take the year or time into account. The only thing that matters is the month and day. So for example an activity could run once a year between September 15th and December 20th.

I need a way to check that the current date is within those two dates taking the day (e.g. 15th) of the month into account. So today's date is October 19 and an activity would be in range if its start date was September 15 and its expiry date December 20, but would be out of range/operation if the expiry date was October 18 or start date was October 25.

So to summarise:

I will be displaying two sections in a table view. One with active activities and another with non-active out of operation activities.

  1. How do take my stored start date and expiry date and easily check the current date is within their range?

  2. For the occasions when users pick either a start date or expiry date I need to check if the current date is greater/less than the start date (to be able to decide if the activity should show up or not) or if the current date is greater/less than the expiry date (to be able to decide if the activity has passed or not passed its expiry date.

It would be nice if I could roll 1 and 2 into one function and use it in the table view.

Would be interested to learn how the more experienced programmer would achieve this.

解决方案

Instead of the date format "MMMM d" (e.g. "September 15"), you can convert the dates to a string with the date format "MMdd" (e.g. "0915"). Then you can do a simple string comparison between the current date, the start date and the expiry date.

For example,

"0915" < "1019" < "1220"

so October 19 is in the range from September 15 to December 20.

这篇关于检查当前日期字符串是否在两个其他日期的范围之内或之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆