Today's date format


#1

Hi,
I’m really new to Kotlin and i’m struggelling
to display today’s date formatted like dd/mm/YYY in a JS project.

Is there a way to go through this without using the magic “js” method ?


#2

Is there a way to go through this without using the magic “js” method ?

It’s possible, but until KT-2506 or KT-20694 are not fixed it requires to write some declarations, like:

(open on try.kotl.in)

external interface JsDateLocaleOptions {
    // The representation of the year. Possible values are "numeric", "2-digit".
    val year: String
    // The representation of the month. Possible values are "numeric", "2-digit", "narrow", "short", "long".
	val month: String
	// The representation of the day. Possible values are "numeric", "2-digit".
	val day: String
}

@JsName("Date")
external class JsDate {
    constructor(s: String)
    
    fun toLocaleDateString(locale: String, options: JsDateLocaleOptions? = definedExternally): String
}

fun main(args: Array<String>) {
    val myoptions = object : JsDateLocaleOptions {
        override val year = "2-digit"
        override val month = "2-digit"
        override val day = "2-digit"
    }
    
    println(JsDate("12/22/2018").toLocaleDateString("en-us", myoptions))
}