Query on dataPicker - ios

Suppose my business hours are from 7am to 7pm, then default selection of time date is :
A) During business hours, a hour later than current time
B) After business hour, 7 am the next business day.
How will you calculate value for Minimum and maximum date ??
Sol -A)
NSDate *minDate = [[NSDate date]dateByAddingTimeInterval:60 * 60 *1];
NSDate *maxDate = [[NSDate date]dateByAddingTimeInterval:60 * 60 * 24 * 2];
Is above solution for (A) is correct ?? is there any other way
Can anyone provide solution for (B) ??
Please provide proper programming code in objective C with explanation ??

Related

NSDateComponents: Get nearest future date

I am creating date components and using the largest unit to display
For example:
1 day 23 hour 2 minute -> 2 days to go (as hour is close to next day
+1 day value)
1 day 10 hour 53 minute -> 1 day to go
0 day 10 hour 50 minute -> 11 hours to go (as minutes are close to next hour +1 hour value)
I'm using below logic to create components
NSCalendar *const calendar = [NSCalendar currentCalendar];
NSDateComponents *const components = [calendar components:(NSCalendarUnitDay |
NSCalendarUnitHour |
NSCalendarUnitMinute)
fromDate:[NSDate date]
toDate:date
options:NSCalendarWrapComponents];
if components.day {return components.day}
if components.hour {return components.hour}
if component.minute {return component.minute}
But above code returns below
1 day 23 hour 2 minute -> 1 day to go
1 day 10 hour 53 minute -> 1 day to go
0 day 10 hour 50 minute -> 10 hours to go
Is there any formatter options that can be used to get rounded up day/hour based upon value of hour/minutes after that?
I know I can check for value of hour if day is present and +1 the day but i'm looking if theres anything supported from iOS

Calculating Coordinated Mars Time v2.0

This is the follow up of a previous question of mine.
In a nutshell, I am trying to follow this tutorial step-by-step: https://jtauber.github.io/mars-clock/ to get to Coordinated Mars Time, but I got stuck right before the end. My code works fine up until the end (some values are more accurate than in the tutorial because I went back to the source from NASA: https://www.giss.nasa.gov/tools/mars24/help/algorithm.html ):
double millis = ( [[NSDate date] timeIntervalSince1970] * 1000 );
NSLog(#"millis: %f", millis);
double JDUT = ( 2440587.5 + (millis / 86400000) );
NSLog(#"JDUT: %f", JDUT);
double JDTT = ( JDUT + (37 +32.184) / 86400);
NSLog(#"JDTT: %f", JDTT);
double J2000Epoch = ( JDTT - 2451545.0 );
NSLog(#"J2000Epoch: %f", J2000Epoch);
double MSD = ( (( J2000Epoch - 4.5 ) / 1.0274912517) + 44796.0 - 0.0009626 );
NSLog(#"MSD: %f", MSD);
The only step remaining is actually calculating Coordinated Mars Time, using this equation:
MTC = mod24 { 24 h × MSD }
The problem is that I have no idea how. I tried to use modf( (double), (double *) ) but no idea how it actually works. I tried it the way below, but it gave me an incorrect answer (obviously as I have really no idea what I am doing). :(
double MSD24 = (24 * MSD);
double MCT = modf(24, &MSD24);
NSLog(#"MCT: %f", MCT); // Result: 0.000000
Any help would be much appreciated. Thank you very much!
p.s.: Notice that I use Objective-C; I do not understand swift unfortunately! :(
Carrying on from the code you gave, I tried:
CGFloat MTC = fmod(24 * MSD, 24);
and got
// 19.798515
which was right according to the web page you cited at the moment I tried it.
The sort of thing his page actually shows, e.g. "19:49:38" or whatever (at the time I tried it), is merely a string representation of that number, treating it as a number of hours and just dividing it up into minutes and seconds in the usual way. Which, I suppose, brings us to the second part of your question, i.e. how to convert a number of hours into an hours-minutes-seconds representation? But that is a simple matter, dealt with many times here. See NSNumber of seconds to Hours, minutes, seconds for example.
So, carrying on once again, I tried this:
CGFloat secs = MTC*3600;
NSDate* d = [NSDate dateWithTimeIntervalSince1970:secs];
NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = #"HH:mm:ss";
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSString* result = [df stringFromDate:d];
NSLog(#"%#", result); // 20:10:20
...which is exactly the same as his web page was showing at that moment.
And here's a Swift version for those who would like to know what the "mean time" is on Mars right now:
let millis = Date().timeIntervalSince1970 * 1000
let JDUT = 2440587.5 + (millis / 86400000)
let JDTT = JDUT + (37 + 32.184) / 86400
let J2000Epoch = ( JDTT - 2451545 )
let MSD = (( J2000Epoch - 4.5 ) / 1.0274912517) + 44796.0 - 0.0009626
let MTC = (24 * MSD).truncatingRemainder(dividingBy: 24)
let d = Date(timeIntervalSince1970: MTC*3600)
let df = DateFormatter()
df.dateFormat = "HH:mm:ss"
df.timeZone = TimeZone(abbreviation: "GMT")!
df.string(from:d)

Dart : Show the time until the next time

How to show a countdown time duration until the next alarm
Code:
TimeOfDay _nextSalah(List<SalahModel> salahs) {
DateTime now = DateTime.now();
List<TimeOfDay> times = [];
int currentSalah;
salahs.forEach((s) => times.add(s.time));
times.add(TimeOfDay(hour: now.hour, minute: now.minute));
times.sort((a, b) => a.hour.compareTo(b.hour));
currentSalah = times.indexWhere((time) => time.hour == now.hour);
return TimeOfDay(hour: times[currentSalah].hour, minute: times[currentSalah].minute);
}
But the time difference is wrong and it doesn't animate. Also how to make sure the time difference works when it's the same day and time of the next day i.e. now is Dec 1 2:30 PM and I want to get the difference on Dec 2 6:15 AM.
It does not work because TimeOfDay represents a time during the day, independent of the date that day might fall on or the time zone. The time is represented only by hour and minute.
If you want a countdown that spans multiple days a DateTime must be used and the time difference evaluation needs some math before formatting the result string, something like:
String nextTime(DateTime nextAlarmTime) {
List<int> ctime = [0, 0, 0, 0];
DateTime now = DateTime.now();
int diff = nextAlarmTime.difference(now).inSeconds;
ctime[0] = diff ~/ (24 * 60 * 60); // days
diff -= ctime[0] * 24 * 60 * 60;
ctime[1] = diff ~/ (60 * 60); // hours
diff -= ctime[1] * 60 * 60;
ctime[2] = diff ~/ 60; // minutes
ctime[3] = diff - ctime[2] * 60; // seconds
return ctime.map((val) => val.toString().padLeft(2, '0')).join(':');
}

iPhone 6 and 6 plus date format #"YYYY-MM-dd'T'HH:mm:ss" does not work unless system date is set to 24 hours in the Settings

On my iPhone 6 and 6 plus, the date format #"YYYY-MM-dd'T'HH:mm:ss" does not work unless system date is set to 24 hours in the Settings.
NSDateFormatter * dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss"];
dateFormater.locale = [NSLocale currentLocale];
NSString *dateString = #"2017-06-12T22:20:04+05:30";
dateFormater.timeZone = [NSTimeZone localTimeZone];
NSDate * startDate = [dateFormater dateFromString:[dateString substringToIndex:19]];
I am getting nil as start date.
Try this :
For 12 Hrs
[dateFormater setDateFormat:#"YYYY-MM-dd'T'hh:mm:ss"];
For 24 Hrs:
[dateFormater setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss"];
Unless you specifically mention the local
dateFormater.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
or otherwise ,in iPhone 6 and 6 plus with iOS version 10.3.2 the conversion of NSString to NSDate will not be give nil value for NSDate. When different local is selected in phone settings.
NSDate * startDate = [dateFormater dateFromString:[dateString substringToIndex:19]];
Its not issue of any particular device. Your dateformater component is wrong.
hh -> This is used for 12 hours formate.
HH -> This is used for 24 hours formate.
Here is brief description about all of them, so you can get idea:
a: AM/PM
A: 0~86399999 (Millisecond of Day)
c/cc: 1~7 (Day of Week)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
d: 1~31 (0 padded Day of Month)
D: 1~366 (0 padded Day of Year)
e: 1~7 (0 padded Day of Week)
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat
EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
F: 1~5 (0 padded Week of Month, first day of week = Monday)
g: Julian Day Number (number of days since 4713 BC January 1)
G~GGG: BC/AD (Era Designator Abbreviated)
GGGG: Before Christ/Anno Domini
h: 1~12 (0 padded Hour (12hr))
H: 0~23 (0 padded Hour (24hr))
k: 1~24 (0 padded Hour (24hr)
K: 0~11 (0 padded Hour (12hr))
L/LL: 1~12 (0 padded Month)
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
LLLL: January/February/March/April/May/June/July/August/September/October/November/December
m: 0~59 (0 padded Minute)
M/MM: 1~12 (0 padded Month)
MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
MMMM: January/February/March/April/May/June/July/August/September/October/November/December
q/qq: 1~4 (0 padded Quarter)
qqq: Q1/Q2/Q3/Q4
qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter
Q/QQ: 1~4 (0 padded Quarter)
QQQ: Q1/Q2/Q3/Q4
QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter
s: 0~59 (0 padded Second)
S: (rounded Sub-Second)
u: (0 padded Year)
v~vvv: (General GMT Timezone Abbreviation)
vvvv: (General GMT Timezone Name)
w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
W: 1~5 (0 padded Week of Month, 1st day of week = Sunday)
y/yyyy: (Full Year)
yy/yyy: (2 Digits Year)
Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)
z~zzz: (Specific GMT Timezone Abbreviation)
zzzz: (Specific GMT Timezone Name)
Z: +0000 (RFC 822 Timezone)

How to convert difference between two times into hours only?

I found the answer to this, but unfortunately it's using Java. I have two times, formatted as HHmm (no colons). I need to figure out how many 15 minute time segments are in the difference. For example, I have a start time of 1000 and an end time of 1130 (military time).
When I subtract the two dates, I get 130, which is meaningless for computations.
Is there an existing method that will do this for me? (I have spent the last 6 hours trying SO and Google, but found nothing).
UPDATE: I would appreciate it if whoever downvoted me please reverse it. The question is very pertinent and others will find it useful. Thank you.
Parse each time and convert to minutes. So 1000 becomes 10 hours 0 minutes for a total of 600 minutes. 1130 becomes 11 hours 30 minutes for a total of 690 minutes. Subtract the two values for a difference of 90 minutes. Now divide by 15 to get 6.
The following assumes all times are 4 digit military times:
NSString *startTime = #"1000";
NSString *endTime = #"1130";
int startMinues = [[startTime substringToIndex:2] intValue] * 60 + [[startTime substringFromIndex:2] intValue];
int endMinues = [[endTime substringToIndex:2] intValue] * 60 + [[endTime substringFromIndex:2] intValue];
int minutes = endMinutes - startMinutes;
int units = minutes / 15;
This gives whole units of your 15 minute blocks.
Use -[NSCalendar components:fromDate:toDate:options], like this:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalenderUnitMinute fromDate:startDate toDate:endDate options:0];
NSInteger numberOfMinutes = [components minute];
Once you have the number of minutes, it should just be a matter of dividing by 15 to get the number of 15 minute chunks.
Try using NSDateComponents:
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [calendar components:NSMinuteCalendarUnit|NSHourCalendarUnit
fromDate:dateA
toDate:dateB
options:0];
int increments = components.hour*4 + components.minute/15;
Format is rather simple:
int HHmm = [date intValue];
int HH = HHmm / 100;
int mm = HHmm % 100;
Diff, for two parsed dates:
int diff = ((HH2 * 60 + mm2) - (HH1 * 60 + mm1)) / 15;

Resources