Swift: (Countdown app) How to make date picker choose the due date? - xcode

Making a countdown app and instead of manually adding the due date i want to be able to do it with a date picker.
Here is the code i am using now. What i need help with is implementing code that let's me do this by using the date picker (The competitionDate).
// Here we set the current date
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Hour, .Minute, .Second, .Nanosecond], fromDate: date)
let hour = components.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day
let currentDate = calendar.dateFromComponents(components)
// here we set the due date. When the timer is supposed to finish
// final Calendar value
let userCalendar = NSCalendar.currentCalendar()
let competitionDate = NSDateComponents()
competitionDate.year = 2015
competitionDate.month = 6
competitionDate.day = 21
competitionDate.hour = 08
competitionDate.minute = 00
let competitionDay = userCalendar.dateFromComponents(competitionDate)!
// Here we compare the two dates
competitionDay.timeIntervalSinceDate(currentDate!)
let dayCalendarUnit = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: date)

you mean to get the date of the date picker?
you can do it like so:
let date:NSDate = datePicker.date

Related

How to convert date format of selected cells using Google Apps Script

I am fairly new to Google Apps Script and I am trying to create a function where it converts the selected cells to a dd/mm/yyyy format. The format the dates are in originally looks like this 2022-01-03 15:00:00 +1100.
I've managed to change the format of the date so it shows up correctly on the logger but when I select the cells and run the function via a menu it just shows up as the last value.
Any help with this would be appreciated. Thank you!!!
function dateconverter(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var selectedContent = ss.getActiveRange().getValues();
var getRange = ss.getActiveRange().getA1Notation()
for(var x =0;x<selectedContent.length;x++){
var fixed = selectedContent[x].join().split(' ');
var dateStr = fixed[0].split('-').join()
var year = +dateStr.substring(0, 4)
var month = +dateStr.substring(5, 7)
var day = +dateStr.substring(8, 10)
var pubdate = [`${day}/${month}/${year}`]
Logger.log(pubdate)
}
ss.getRange(getRange).setValue(pubdate)
}
Use :- Utilities.formatDate(new Date(),Session.getTimeZone,"dd/MM/yyyy")

Datepicker values only change when changing date

Is it possible to have datepicker land on todays date when the app starts?
currently my app shows no values on start up and only when I start changing the date in my datepicker then values start appearing. datepicker outputs values to labels
Which programming language do you use?
This code is for swift3 ;
You should write it in viewDidLoad()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
yourTextField.text = dateFormatter.string(from: (NSDate() as Date))

Date Picker Now. Cocoa-OSX Swift

How can I make the date picker show the date and time that is now? Without clicking nothing, just at the beginning. I am very new in Cocoa, I would appreciate any orientation.
This is what I have done:
- I create a new project > OSX > Application > Cocoa Application
- Main.storyboard. Drag a Date Picker
- Ctrl + drag from date picker to ViewController.swift. I give it the name datePicker: #IBOutlet weak var datePicker: NSDatePicker!
Add [datePicker setDateValue:[NSDate new]]; in viewdidLoad of View Controller
Here is a function that returns the hour and the day and corrects for different time zones, I am using 7 for Nevada PST time zone but you can update for your specific state
func returnHourDay() -> (Int, Int) {
let dateNow = Date()
let calendar = Calendar.current
var Hour = (calendar as NSCalendar).component(NSCalendar.Unit.hour, from: dateNow)
let Day = (calendar as NSCalendar).component(NSCalendar.Unit.weekday, from: dateNow)
//correct for specific time zones
func ltzOffset() -> Int { return NSTimeZone.local.secondsFromGMT() }
let hourOffset = 7 + ltzOffset()/60/60 //0 for Nevada 7 - 7 = 0
Hour = Hour+hourOffset
return (Hour, Day)
}

How to get weekends vs weekdays in Cocoa?

What is the correct calendar-agnostic way (in either iOS or OS X) to get standard business days of the week (e.g. weekdays) versus non-working days of the week? This is taking into account that some parts of the world doesn't follow the Monday through Friday working day routine. E.g. the UAE and some parts of Malaysia works from Sunday through Thursday.
I'm looking to emulate iOS' built-in Alarm application in which if I select a repeating day of Monday through Friday, it will say that my alarm repeats "Weekdays" whereas for alarms that fires on Saturday and Sunday says "Weekends". However, I'd like my app to also work correctly in other parts of the world that doesn't follow the western world's work week.
I've been toying around with NSCalendar's isDateInWeekend function with various other calendars and haven't been able to find a good method to determine weekday vs weekend. Here's a snippet from a Swift playground that I got so far:
import UIKit
let arabicLocale = NSLocale(localeIdentifier: "en_AR")
let islamicTabularCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicTabular)
islamicTabularCalendar?.locale = arabicLocale
let islamicTabularFormatter = NSDateFormatter()
islamicTabularFormatter.calendar = islamicTabularCalendar
islamicTabularFormatter.dateStyle = NSDateFormatterStyle.FullStyle
let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
gregorianCalendar?.locale = arabicLocale
let gregorianFormatter = NSDateFormatter()
gregorianFormatter.calendar = gregorianCalendar
gregorianFormatter.dateStyle = NSDateFormatterStyle.FullStyle
let monday = NSDateComponents()
monday.weekday = 2
monday.weekOfMonth = 0
let friday = NSDateComponents()
friday.weekday = 6
friday.weekOfMonth = 0
let mondayDate = islamicTabularCalendar?.dateFromComponents(monday)
gregorianFormatter.stringFromDate(mondayDate!) // "Monday, July 8, 622"
islamicTabularFormatter.stringFromDate(mondayDate!) // "Monday, Dhuʻl-Hijjah 20, 0 AH"
islamicTabularCalendar?.isDateInWeekend(mondayDate!) // false
let fridayDate = islamicTabularCalendar?.dateFromComponents(friday)
gregorianFormatter.stringFromDate(fridayDate!) // Friday, July 12, 622
islamicTabularFormatter.stringFromDate(fridayDate!) // Friday, Dhuʻl-Hijjah 24, 0 AH
islamicTabularCalendar?.isDateInWeekend(fridayDate!) // false
What I'm looking for is something that returns false for Fridays in parts of the world that practices Monday-Friday working days but returns true for UAE and the like.
I finally got it. The mistake was in the locale identifier which should be en_AE instead of en_AR
import UIKit
let arabicLocale = NSLocale(localeIdentifier: "en_AE")
let islamicTabularCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicTabular)
islamicTabularCalendar?.locale = arabicLocale
let islamicTabularFormatter = NSDateFormatter()
islamicTabularFormatter.calendar = islamicTabularCalendar
islamicTabularFormatter.dateStyle = NSDateFormatterStyle.FullStyle
let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
gregorianCalendar?.locale = arabicLocale
let gregorianFormatter = NSDateFormatter()
gregorianFormatter.calendar = gregorianCalendar
gregorianFormatter.dateStyle = NSDateFormatterStyle.FullStyle
let monday = NSDateComponents()
monday.weekday = 2
monday.weekOfMonth = 0
let friday = NSDateComponents()
friday.weekday = 6
friday.weekOfMonth = 0
let mondayDate = islamicTabularCalendar?.dateFromComponents(monday)
gregorianFormatter.stringFromDate(mondayDate!) // "Monday, July 8, 622"
islamicTabularFormatter.stringFromDate(mondayDate!) // "Monday, Dhuʻl-Hijjah 20, 0 AH"
islamicTabularCalendar?.isDateInWeekend(mondayDate!) // false
gregorianCalendar?.isDateInWeekend(mondayDate!) // false
let fridayDate = islamicTabularCalendar?.dateFromComponents(friday)
gregorianFormatter.stringFromDate(fridayDate!) // Friday, July 12, 622
islamicTabularFormatter.stringFromDate(fridayDate!) // Friday, Dhuʻl-Hijjah 24, 0 AH
islamicTabularCalendar?.isDateInWeekend(fridayDate!) // true
gregorianCalendar?.isDateInWeekend(fridayDate!) // false

How to set time to a variable without the date

I am trying to set a specific time to two variables but can't seem to correctly format the syntax. I want to be able to set Shift 1 and Shift 2 off of certain times indicated below.
I want to only be able to use these times in an IF statement so that a radio button can be checked by default. Day Shift button and a Night Shift button. If the current time is in between shift 1, then day shift button is checked.
Date.prototype.currentTime = function(){
return ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ this.getMinutes()+((this.getHours()>12)?('PM'):'AM'); };
var d1= new Date();
var d2 = new Date();
d1.setHours(7);
d1.setMinutes(10);
d2.setHours(19);
d2.setMinutes(10);
alert(d1.currentTime());
alert(d2.currentTime());
Thanks Any help is appreciated.
You do not need to compare Date objects for your use,
just compare the hours as integers to the integer from now.getHours():
var now= new Date().getHours();
if(now>6 && now<19){
//check day shift button;
}
// else check niteshift button
You may try this:
http://jsfiddle.net/apq59j9u/
Date.prototype.currentTime = function(){
return ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ this.getMinutes()+((this.getHours()>12)?('PM'):'AM'); };
var d1= new Date();
var d2 = new Date();
d1.setHours(7);
d1.setMinutes(10);
d2.setHours(19);
d2.setMinutes(10);
alert(d1.currentTime());
alert(d2.currentTime());
Getting the Data
There are a number of different ways to do this. First here is a way to get the data from the date object:
var d = new Date();
var n = d.toTimeString();
Unfortunately, this will output something like "12:30:30 GMT-0500 (Eastern Standard Time)"
You can also try the getHours() and getMinutes functions to get the hours and minutes in your current timezone.
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
Setting the Data
This is pretty easy to do, just as getting the data from a date object. Use the following code to set the time to what you would like. Replace the numbers where you see 11 to conform to your needs.
NOTE: This time is in military style hours! 1 = 1am, 13 = 1pm, ect.
var d = new Date();
d.setHours(11);
d.setMinutes(11);
d.setSeconds(11);
Result: 11:11:11

Resources