Use 2017-02-12T13:30:00Z date in angular2-simple-countdown/countdown - date

I want to make a Count down timer using Angular 2 .
I am able to get the Timer using the ans provided to work but i am not able to override the Style of the Timer the Size of the timer i pretty big.
Using like below in template.
<count-down [text]="text" units="Days | Hours | Minutes | Seconds " end="{{fixture.date}}"></count-down>

howto binding data to your CountDownTimer component, your date from api could be using like this
let d = new Date('2017-02-12T13:30:00Z');
let year = d.getFullYear();
let month = d.getMonth();
let day = d.getDate();
from there you could binding any property you want.

Related

Unable To Set Date in DatePicker Swift

I have a common datePicker,
where in Part 1 of the code (which is executed when datePicker is changed) I am setting datePicker's minimumDate and maximumDate and this works.
In Part 2 of the code (which is executed when cell is tapped) I am only setting the datePickers date.
When Part 1 is followed by Part 2 of the code I am unable to set datePickers date i.e the datePicker shows the maximumDate set (which is done in part 1 of the code)
When Part 2 of the code executed without part 1, I am able to set the datePickers date.
Can someone please advice where I could be going wrong ?
Part 1
let endTimeString = self.timesArray[row]["endTime"]!
let endTimeObject = self.convertToDateObject(timeString: endTimeString)
let maxStartTimeObject = endTimeObject.addingTimeInterval(-5 * 60.0) // removing 5mins
cell.datePicker.maximumDate = maxStartTimeObject
let startTimeString = self.timesArray[row]["startTime"]
let startTimeObject = self.convertToDateObject(timeString: startTimeString!)
let minEndTimeObject = startTimeObject.addingTimeInterval(5 * 60.0) // adding 5mins
cell.datePicker.minimumDate = minEndTimeObject
Part 2
cell.datePicker.date = dateObject
Is it possible that the date you are trying to set it outside your min/max bounds?

How do I find the time interval remaining from NSTimer

I have set a NSTimer.scheduledTimerWithTimeInterval method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the interval?
Thanks
You have access to a NSTimer's fireDate, which tells you when the timer is going to fire again.
The difference between now and the fireDate is an interval you can calculate using NSDate's timeIntervalSinceDate API.
E.G. something like:
let fireDate = yourTimer.fireDate
let nowDate = NSDate()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)
When using the Solution of #Michael Dautermann with normal Swift type Timer and Date I have noticed, that his solution will give you a negative value e.g.:
let fireDate = yourTimer.fireDate
let nowDate = Date()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)
//above value is negative e.g. when the timers interval was 2 sec. and you
//check it after 0.5 secs this was -1,5 sec.
When you insert a negative value in the Timer.scheduledTimer(timeInterval:,target:,selector:,userInfo:,repeats:) function it would lead to a timer set to 0.1 milliseconds as the Documentation of the Timer mentions it.
So in my case I had to use the negative value of the nowDate.timeIntervalSinceDate(fireDate) result: -nowDate.timeIntervalSinceDate(fireDate)

How can I get current date and time in swift with out Foundation

I am trying to do the equivalent of NSDate() but with out importing Foundation.
Does the Darwin module have a way to do this?
I was looking at this answer but no dice
How can I get a precise time, for example in milliseconds in Objective-C?
I am not sure if this is what you are looking for, but the BSD library
function
let t = time(nil)
gives the number of seconds since the Unix epoch as an integer, so this is almost the same as
let t = NSDate().timeIntervalSince1970
only that the latter returns the time as a Double with higher
precision. If you need this higher precision then you could use
gettimeofday():
var tv = timeval()
gettimeofday(&tv, nil)
let t = Double(tv.tv_sec) + Double(tv.tv_usec) / Double(USEC_PER_SEC)
If you are looking for the time broken down to years, month, days, hours etc according to your local time zone, then use
var t = time(nil)
var tmValue = tm()
localtime_r(&t, &tmValue)
let year = tmValue.tm_year + 1900
let month = tmValue.tm_mon + 1
let day = tmValue.tm_mday
let hour = tmValue.tm_hour
// ...
tmValue is a struct tm, and the fields are described in
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/localtime.3.html.

How to display seconds in year, day format in swift

All i need to do is to change the way that xcode displaying my values in. My code currently displaying the age of something in minutes e.g 4503mint . I want to be able to display these values in the following format 3 days 3 hours 3 mint rather than 4503 mint. Really appreciate your help. Regards
You said:
My code currently displaying the age of something in minutes e.g 4503mint . I want to be able to display these values in the following format 3 days 3 hours 3 mint rather than 4503 mint.
You can format this using NSDateComponentsFormatter. Just multiply the number of minutes by 60 to get the NSTimeInterval, and then you can supply that to the formatter:
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full
let minutes = 4503
let timeInterval = NSTimeInterval(minutes * 60)
print(formatter.stringFromTimeInterval(timeInterval))
That will produce "3 days, 3 hours, 3 minutes" (or in whatever format appropriate for the locale for that device).
See NSDateComponentsFormatter Reference for more information.
You can obviously calculate it yourself (i.e. days = seconds/(3600*24)etc.), you can also look at NSDateComponentsFormatter, which may be exactly the functionality you are looking for with almost no coding effort.
You could calculate it yourself or do something like this:
let mySeconds = 700000
let date = NSDate(timeIntervalSinceNow: mySeconds) // difference to now
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate("yyyy.MM.dd", options: 0, locale: NSLocale.currentLocale())
dateFormatter.stringFromDate(date)

Set maximum time with UIDatePicker - time mode - swift

Is it possible to customise the UIDatePicker in CountDownTimer mode to not show the picker values past a certain point? For example if I set it to 2 hours it would only show 2 hours downwards.
Currently I have tried a few techniques but all I have succeeded in is setting the countdown point to a certain value using maximumDate.
self.datePickerView.datePickerMode = UIDatePickerMode.CountDownTimer
//For calculating a date with +30 mins
let calendar = NSCalendar.currentCalendar()
let date = calendar.dateByAddingUnit(.CalendarUnitMinute, value: 30, toDate: NSDate(), options: nil)
println("30+Mins \(date)")
//Interval for countdownDuration
let myTimeInterval = NSTimeInterval(timeOffset * 60)
println("myTimeInterval \(myTimeInterval)")
//Tried Methods - countDownDuration sets it to a certain points(in seconds)
self.datePickerView.countDownDuration = myTimeInterval
self.datePickerView.maximumDate = date // This doesn't seem to do anything when in using UIDatePickerMode.CountDownTimer
I presume a possible way is to use just a UIPicker.
Exactly as you say - it's possible to do it yourself with UIPickerView. And that's actually exactly what I did: I built a custom UIPickerView subclass for that purpose. It replicates the functionality of UIDatePicker in countDownTimer mode, while adding support to set maxTimeInterval.
You use it like this:
GSTimeIntervalPicker *picker = [[GSTimeIntervalPicker alloc] init];
picker.maxTimeInterval = (3600 * 3); // set the limit
picker.minuteInterval = 5; // the step. Default is 1 min.
picker.timeInterval = (3600 * 1.5); // 1 h 30 minutes
picker.onTimeIntervalChanged = ^(NSTimeInterval newTimeInterval) {
// Use the value
};
Available on GitHub under MIT license. Blog post here. I'm sorry it's not in Swift, you will have to convert it yourself.
First Thing: you don't need a calendar to add 30 minutes to a given date.
There is a constructor that does what you want in one step:
NSDate(timeInterval: NSTimeInterval, sinceDate: NSDate)
NSDate(timeIntervalSinceNow: NSTimeInterval)
for me,
self.datePickerView.maximumDate = date
shows future dates / time greyed out, so the user has some visual hint.
This doesn't mean, a future date / time is not selectable without additional code.
The user can select a greyed out time. You can check in your handler and when some future date is selected, you just set
self.datePickerView.date = date
as long as you don't call resignFirstResponder(), your date picker scrolls back to now (or any other chosen time) nicely. So the user is not able to select a time in the future with this additional code.
I hope this is near enough to what you need. Future is greyed out instead of invisible and it is not selectable with a little additional code in your handler.