I'm trying to pass a date to a function in Kotlin. I don't want to have to type "08/30/2018" as a String and would instead prefer Date. I initially tried just 08/30/2018 but get a compiler stating an Integer was found for the input rather than Date.
var myDate = 1/1/2000
var newDate = 08/30/2018
fun setDate(value: Int){
myDate = value
}
setDate(newDate)
println(newDate) //0
println(myDate) //0
println(myDate.compareTo(newDate)) //0
Why does Kotlin accept 08/30/2018 as an int? Why is it stored correctly to another int variable but then print 0 when the value is retrieved?
How can I initialize a variable to a date like 1/1/2000 and then set another date later on? I haven't found anything about passing a date anywhere unless it is a String.
When you assign 8/30/2018 to a variable then the compiler recognizes it as integer division 8/30 which is 0 and then /2018 and the result is 0.
You could do it like val date = Date("1/1/2000") but this is now deprecated
You can use the Java 8 Date/Time API instead:
val date = LocalDate.of(2000, 1, 1)
you can find more here:
https://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
Related
I have three date objects. I want to know, which of them is the oldest. I found a lot about how to compare two dates, but not multiple...
So I have my three date variables:
var firstDate:Date!
var secondDate:Date!
var thridDate:Date!
and I assign dates to them:
firstDaten = dateOne
secondDate = dateTwo
thridDate = dateThree
How can I find out in Swift, which of my dates is the oldest?
Tanks for help!
As Date conforms to Comparable, create an array and use the min() function
let dates = [firstDate, secondDate, thridDate]
let oldest = dates.min()
Note:
It is not necessary to declare a variable as implicit unwrapped optional if a value is assigned right after the declaration, this compiles:
let firstDate : Date
firstDate = dateOne
I'm very new to swift.
How to set empty value for Date type in Swift to be used in declarations?
Like:
var string: String = "" for String type
var integer: Int = 0 for Int type.
Thank you.
From the documentation :
A Date value encapsulate a single point in time, independent of any
particular calendrical system or time zone. Date values represent a
time interval relative to an absolute reference date.
In other terms, a date is nothing but a TimeInterval, A.K.A a Double representing the number of seconds since a reference date. Then, an empty initializer for Date would be the one that returns a date 0 seconds away from that reference date. It all comes down to choosing the reference date :
Now : let date1 = Date()
Jan 1, 1970 at 12:00 AM : let date2 = Date(timeIntervalSince1970: 0)
Jan 1, 2001 at 12:00 AM : let date3 = Date(timeIntervalSinceReferenceDate: 0)
You can choose a certain date as a reference if it makes sense to you. Examples: Date.distantPast, Date.distantFuture, ...
Now, if you want a variable to have a certain type, but no value, then use optionals and set their values to nil:
var date4: Date? = nil
Later on, when you want to actually use the variable, just set it to a non-nil value:
date4 = Date(timeInterval: -3600, since: Date())
To use the actual value, you'll have to unwrap it using optional binding or the likes of it.
This is a very simple question but it's been bugging me. According to Swift, it's not possible for values of different types to be added together (like String and Int), how's it possible that the following code works even though it adds type Date and type Int together?
let someDate = Date() + 2828282
On the documentation page for Date you can see
that there is a +
operator taking a Date and TimeInterval as operands:
Returns a date with a specified amount of time added to it.
static func +(lhs: Date, rhs: TimeInterval) -> Date
So you can add a TimeInterval (which is a type alias for Double)
to a Date, but not an Int. Your code compiles because 2828282
is a "number literal" and the compiler can infer the type from
the context as Double.
This would not compile:
let delta = 2828282 // an Int
let someDate = Date() + delta
// error: binary operator '+' cannot be applied to operands of type 'Date' and 'Int'
You would have to convert the Int to a TimeInterval/Double
let delta = 2828282 // an Int
let someDate = Date() + TimeInterval(delta)
or make delta a Double:
let delta = 2828282.0 // a Double
let someDate = Date() + delta
According to Swift, it's not possible for values of different types to be added together (like String and Int)
Unlike in many other languages, there is no automatic conversions between numeric types, a.k.a numeric promotions, and the majority of pre-defined numeric operators expect both operands to be of the same type.
However there is nothing in Swift to prevent operators being declared with different type operands, and your example Date() + 2828282 is one example where a pre-defined operator is defined with different operand types.
So the difference with Swift is that is lacks automatic numeric promotions, and that combinations such realVar + intVar are not pre-defined.
In many other languages the combination is not pre-defined either, but numeric promotions exist, so the example is compiled as realVar + real(intVar).
HTH
I have a Date string of following format:
'31-OCT-2013'
How do I convert this into Date of following format using Extjs 4:
'08/31/2013'
I am using IE8.
If you have string "31-OCT-2013", you need to:
Convert it into date object
var myDate = Ext.Date.parse("31-OCT-2013", 'd-M-Y');
Format it to as you want
Ext.Date.format(myDate, 'm/d/Y');
Try something like this:
If your day representation would be 2 digit with leading zero, then apply this
var date = Ext.Date.parse("31-OCT-2013", 'd-M-Y');
console.log(Ext.Date.format(date, 'm/d/Y'));
But if your day representation would be without a leading zero, then apply this
var date = Ext.Date.parse("31-OCT-2013", 'j-M-Y');
console.log(Ext.Date.format(date, 'm/d/Y'));
Check the docs for Ext.Date
I am using ColdFusion 9.0.1 and some database that I cannot change.
I am accessing a database that stores a date as an eight digit numeric with zero decimal places like this:
YYYYMMDD
I need to be able to read the date, add and subtract days from a date, and create new dates. I am looking for a ColdFusion solution to efficiently (not much code) to convert the date to our standard format, which is
MM/DD/YYYY
And then convert it back into the database's format for saving.
I need to code this in such a way that non-ColdFusion programmers can easily read this and use it, copy and modify it for other functions (such as adding a day to a date). So, I am not looking for the most least amount of code, but efficient and readable code.
Can you suggest anything that would make this code block more flexible, readable, or more efficient (less code)?
<cfscript>
// FORMAT DB DATE FOR BROWSER
DateFromDB = "20111116";
DatedToBrowser = createBrowserDate(DateFromDB);
writeOutput(DatedToBrowser);
function createBrowserDate(ThisDate) {
ThisYear = left(ThisDate, 4);
ThisMonth = mid(ThisDate, 4, 2);
ThisDay = right(ThisDate, 2);
NewDate = createDate(ThisYear, ThisMonth, ThisDay);
NewDate = dateFormat(NewDate, "MM/DD/YYYY");
return NewDate;
}
// FORMAT BROWSER DATE FOR DB
DateFromBrowser = "11/16/2011";
DateToDB = createDBDate(DateFromBrowser);
writeDump(DateToDB);
function createDBDate(ThisDate) {
ThisYear = year(ThisDate);
ThisMonth = month(ThisDate);
ThisDay = day(ThisDate);
NewDate = "#ThisYear##ThisMonth##ThisDay#";
return NewDate;
}
</cfscript>
First find who ever did the database and kick them in the nads...
Personally I'd Convert with sql so my code only dealt with date objects.
Select Convert(DateTime, Convert(VarChar(8),DateTimeInventedByIdjitColumn))
From SomeTable
As stated by our peers, store dates as dates.
'08/06/2011' could be 8th of june of the 6th of August depending on locale.
20111643 is a valid integer..
Not using a proper date type is just a massive collection of features and bugs that at best are waiting to happen.
You can actually rewrite each function into 1 line of code.
function createBrowserDate(ThisDate) {
return mid(ThisDate,4,2) & "/" & right(ThisDate,2) & "/" & left(ThisDate,4);
}
and
function createDBDate(ThisDate) {
return dateFormat( ThisDate, "YYYYMMDD" );
}
Don't keep dates as strings - keep dates as dates and format them when you need to.
If you can't correct the database to use actual date columns (which you should if you can), then you can use these two functions to convert to/from YYYYMMDD and a date object:
function parseYMD( YYYYMMDD )
{
if ( ! refind('^\d{8}$' , Arguments.YYYYMMDD ) )
throw "Invalid Format. Expected YYYYMMDD";
return parseDateTime
( Arguments.YYYYMMDD.replaceAll('(?<=^\d{4})|(?=\d{2}$)','-') );
}
function formatYMD( DateObj )
{
return DateFormat( DateObj , 'yyyymmdd' );
}
By using date objects it means that any level of developer can work with them, without needing to care about formatting, via built-in functions like DateAdd, DateCompare, and so on.
I'm not a regular expression fan since it's not that readable to me.
Since you're using CF9, I'd typed the argument and specify the returntype of the functions to be even more readable for the next person picking up your code.
First, right after I read the date from DB, I'd parse it to a Date object using parseDBDate()
Date function parseDBDate(required String dbDate)
{
var yyyy = left(dbDate, 4);
var mm = mid(dbDate, 4, 2);
var dd = right(dbDate, 2);
return createDate(yyyy , mm, dd);
}
Once you have the date object, you can use all those built-in Date functoin like DateAdd() or DateDiff().
Call browserDateFormat() right before you need to display it.
String function browserDateFormat(required Date date)
{
return dateFormat(date, "MM/DD/YYYY");
}
Call dBDateFormat() inside <cfqueryparam value=""> when it's time to persist to DB
String function dBDateFormat(required Date date)
{
return dateFormat(date, "YYYYMMDD");
}
One liner :)
myDateString = "20110203";
myCfDate = createObject("java","java.text.SimpleDateFormat").init("yyyyMMdd").parse(myDateString,createObject("java","java.text.ParsePosition").init(0*0));
If you want to parse different patterns, change "yyyyMMdd" to any other supported pattern.
http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
The ParsePosition is used to say where to start parsing the string.
0*0 is shorthand for JavaCast("int",0) - in the Adobe cf engine, 0 is a string, until you apply math to it, then it becomes a Double, which the ParsePosition constructor supports. Technically, it constructs with an int, but cf is smart enough to downgrade a Double to an int.
http://download.oracle.com/javase/1.5.0/docs/api/java/text/ParsePosition.html