如何将日期从给定日期分成半年?

时间:2016-01-30 07:05:10

标签: jquery date textbox

例如,

if let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) {
  var thisWeek = [NSDate]()
  for i in 0...6 {
    if let nextDay = calendar.dateByAddingUnit(NSCalendarUnit.Day , value: i, toDate: NSDate(), options: .MatchStrictly) {
      thisWeek.append(nextDay)
    }
  }
}

给出Textbox中的两个日期。

First Half --- 01 Jun 2015 to 30 Nov 2015
Second Half --- 01 Dec 2015 to 31 May 2016

此处根据From Date --- 01 Nov 2015 To Date --- 15 Feb 2016 First half分割给定日期。

预期输出为:

Second half

3 个答案:

答案 0 :(得分:0)

这是用于计算上半部分的代码,不使用任何库。我想你应该能够弄清楚下半场的其余部分:

var currDate = new Date();
var tmp = new Date(currDate.getTime());
var lowerFirstHalf = new Date(tmp.setMonth(currDate.getMonth()-6));

lowerFirstHalf = new Date(lowerFirstHalf.getFullYear(),lowerFirstHalf.getMonth(),1);

tmp = new Date(lowerFirstHalf.getTime());
var upperFirstHalf = new Date(tmp.setMonth(tmp.getMonth()+6));

upperFirstHalf = new Date(upperFirstHalf.getFullYear(),upperFirstHalf.getMonth()+1,0);

console.log("Current date: "+currDate);
console.log("Lower First half: "+lowerFirstHalf);
console.log("Upper First half: "+upperFirstHalf);

结果:

Current date: Sat Jan 30 2016 08:38:11 GMT+0100 (CET) _display:56:1
Lower First half: Wed Jul 01 2015 00:00:00 GMT+0200 (CEST) _display:57:1
Upper First half: Sun Jan 31 2016 00:00:00 GMT+0100 (CET) _display:58:1

或者查看这个jfiddle:http://jsfiddle.net/8w8v9/973/

答案 1 :(得分:0)

       function FormatDate(inputdate) {
          var date = inputdate.getDate();
          var month = inputdate.getMonth() + 1;
          var year = inputdate.getFullYear();
          return date + '/' + month + '/' + year;
       }

        function HalfYearlyDate() {
        var firstHalfstart = new Date("01 Jun 2015") //First half start date
        var firstHalfend = new Date("30 Nov 2015") // First half end date

        var secondHalfStart = new Date("01 Dec 2015")//second half start date
        var secondHalfEnd = new Date("31 May 2016")// second half end date

        var date1 = new Date("01 Nov 2015"); // input first half date
        var date2 = new Date("15 Feb 2016"); // // input second half date

        //Please format the date using a custom function or using moment.js before printing
        if (date1.getTime() >= firstHalfstart.getTime() && date1.getTime() <= firstHalfend.getTime()) {

            if (date1.getTime() <= date2.getTime()) {
                console.log("First Half --- From Date : " + FormatDate(date1) + " and To Date :" + FormatDate(date2));
            }
            else {
                console.log("First Half --- From Date : " + FormatDate(date1) + " and To Date :" + FormatDate(firstHalfend));
            }
        }
        else {
            console.log("some error message");
        }

        if (date2.getTime() >= secondHalfStart.getTime() && date2.getTime() <= secondHalfEnd.getTime()) {

            if (date2.getTime() <= secondHalfEnd.getTime()) {
                console.log("Second  Half --- From Date : " + FormatDate(secondHalfStart) + " and To Date :" + FormatDate(date2));
            }
            else {
                console.log("Second  Half --- From Date : " + FormatDate(secondHalfStart) + " and To Date :" + FormatDate(secondHalfEnd));
            }
        }
        else {
            console.log("some error message");
        }    
   }

希望上面的代码能为您提供解决问题的见解

答案 2 :(得分:0)

完美的工作

@Override
protected Notification getNotification(Context context, Intent intent) {
    final Notification notification = super.getNotification(context, intent);
    // TODO Update this notification here
    return notification;
}

@Override
protected void onPushOpen(Context context, Intent intent) {
    //TODO For app specific implementation comment the following line calling super version of this method
    // super.onPushOpen(context, intent);

    //TODO Add App Specific implementation below
    Intent lastIntent = new Intent(context,MainPageActivity.class);
    lastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(lastIntent);
}