How to store NSDate plus 7 calendar days? - nsdate

I need to be able to store the current NS Date plus 7 days. I already store the current NSDate. I then want to compare the two dates, to get the number of days difference between the two and display within a label. The idea is to count down from 7 days, to 6 days, to 5 days, etc until it shows 0 and i will remove it from my tableview.
I have been having hard time finding the write code for this, so any help would be greatly appreciated!!!

This code will compute date after 7 days from today.
NSDateComponents *comps = [NSDateComponents new];
comps.day = 7;
NSDate *today = [NSDate date];
NSCalendar *c = [NSCalendar currentCalendar];
NSDate *dateAfter7days = [c dateByAddingComponents:comps toDate:today options:0];
For Your date diffrence the code is:-
// c is calendar object
NSDateComponents *comps = [c components:NSDayCalendarUnit fromDate:startDate toDate:currentDate options:0];
int diffrence = [comps day];

Related

groovy date plus giving wrong dates

I am trying to generate a random date and then add 5 number of days to it. it works fine most of the times but when the new date goes into next year, the result is actually going back to the beginning of the same year. Not sure why.. here is my code
Date today = new Date();
def endRange = 1500
def randomInterval = new Random().nextInt(endRange)
startDate = today.plus(randomInterval)
endDate= startDate.plus(5)
I run this in loop for 1000 times and 18 out of those are either resetting back to the same year or adding one year. not sure why. Below are two example output results
startdate enddate
2022-12-27 -- > 2022-01-01
2020-12-26 -- > 2021-12-31
appreciate any help on this
// update
after a bit digging, this is happening after I change the date format to "YYYY-MM-dd" before writing to excel as that is the format I need. and it is consistently happening to any date that gets rolled over to next year after adding 5 days.
Date today = new Date();
def minStart = today+5 //diff_currentDt_startDt
def endRange = 1500
def randomInterval = new Random().nextInt(endRange)
startDate = today.plus(randomInterval)
endDate= startDate.plus(diff_startDt_endDt)
// after this I format them and then write to excel
def startDate1=startDate.format('YYYY-MM-dd')
def endDate1=endDate.format('YYYY-MM-dd')
Label label1= new Label(0, i, startDate1)
sheet.addCell(label1)
Label label2= new Label(1, i, endDate1);
sheet.addCell(label2);
again checking further, somehow the format is messing it up. Below are the dates before and after formatting
Tue Dec 24 19:31:02 EST 2019 //startdate
2019-12-24 // startdate in YYYY-MM-dd format
Sun Dec 29 19:31:02 EST 2019 // enddate (startdate +5)
2020-12-29 // enddate in YYYY-MM-dd format
what am I doing wrong?
looks like the "YYYY" in .format('YYYY-MM-dd') is causing issue its fixed after changing it to "yyyy" in .format('yyyy-MM-dd')

MomentJS startOf('day') seem to be 12 AM (12 in 24-hour format)?

I have a date stored in YYYY-MM-DD. This date is used to create a moment called then. I want to count days between this date and now (moment).
When counting days I use: now.startOf('day').from(then.startOf('day')).
As an example, when testing using two dates with 3 days apart, it will be 3 days until 12 AM (12 in 24-hours format).
Do you guys know any idea why?
Just wanted to update this.
The beware the difference between HH and hh.
HH is 24 hours so the start of day is 12 where as HH the start is 0.
.format('HH:mm:ss') returns 00:00:00
.format('hh:mm:ss') returns 12:00:00
See (moment docs)[http://momentjs.com/docs/#/displaying/difference/].
Your example:
var moment = require('moment');
var a = moment("2014-05-02");
var b = moment();
var whole = a.diff(b, 'days');
var fract = a.diff(b, 'days', true);
console.log("Difference is " + whole + ", or more precisely, " + fract );
Returns:
Difference is 1, or more precisely, 1.404142025462963

Week number of a year in dateformat

I need to get the year I use the format YYYY, and month MM, and so on. I am looking to see if there is a format to get the week number of the year.
The date that I am using as example is 2008-03-09 16:05:07.000, and I would like to know the week number which is 11 in this case. Is there a way to retrieve 11 programmatically?
There's no format that provides the weeknumber, but you can get it in this way easily:
System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
int weekNum = cul.Calendar.GetWeekOfYear(
DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
CultureInfo.Calendar Property
Calendar.GetWeekOfYear Method
Custom Date and Time Format Strings
To answer your comment how to get the quarter of a year of a given DateTime:
int quarter = (int)((DateTime.Now.Month - 1) / 3) + 1

NSPredicate and CoreData - decide if a Date attribute is "today" (or between last night 12am to tonight 12am) on iOS

I'm using a NSFetchedResultsController and a UITableViewController to populate a UITableView from a CoreData database.
I have a NSDate object saved into this Date attribute labeled "startTime". Then I'm trying to only pull todays's data by using a NSPredicate that looks like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"startDate == %#",
todaysDate];
I'm getting zero results. I understand this because a NSDate object just hold the number of seconds or milliseconds or whatever since Jan 1 1970 right? So comparing 1111111111111 and 1111111111112, while on the same day, are two distinct NSDate objects that aren't equal.
So, how could the NSPredicate be formatted so that it would do it? I'm going to guess its creating two NSDate objects: one that is at 12am last night, and another for 12am tonight and compare startDate and see if its between these two NSDates.
I'm kind of new to NSPredicate, so how could this be accomplished?
Use NSCompoundPredicate.
NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:#"startDate > %#", firstDate];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:#"startDate < %#", secondDate];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:firstPredicate, secondPredicate, nil]];
Where firstDate is 12am this morning and secondDate is 12am tonight.
P.S. Here's how to get 12am this morning:
NSCalendar *calendar = [NSCalendar calendar]; // gets default calendar
NSCalendarComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, and day for today's date
NSDate *firstDate = [calendar dateFromComponents:components]; // makes a new NSDate keeping only the year, month, and day
To get 12am tonight, change components.day.

stringFromDate returning the wrong year

#define stdDateFormat #"YYYYMMdd'T'hh:mm:ssZ"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:stdDateFormat];
NSString *TimeOfSync = [dateFormat stringFromDate:syncDate];
NSLog(#"date format: %#",stdDateFormat);
NSLog(#"syncDate: %#",syncDate);
NSLog(#"TimeOfSync: %#",TimeOfSync);
Logged output:
date format: YYYYMMdd'T'hh:mm:ssZ
syncDate: 2009-01-03 19:00:00 +0000
TimeOfSync: 20080103T11:00:00-0800
Can anyone help? The only thing I can see is the extra space just before the timezone in the input date string. Thanks.
Use lowercase yyyy for the year instead of YYYY.
Uppercase Y means the year of the start of the week the date is in (Jan 3, 2009 is in a week that starts in 2008).
See Unicode Date Format Patterns.

Resources