karen
karen

Reputation: 43

Convert yyyy-mm-dd hh:mm:ss to day and month

I've got dates in string format in my qml/javascript app. I would like to convert 2016-01-30 12:00:00 to something short and clear like 30 Jan 2016 12h. Is there any way to do that?

Upvotes: 0

Views: 1416

Answers (1)

iBelieve
iBelieve

Reputation: 1544

This is very easy to do in QML. Here's a sample that prints the exact string you wanted:

import QtQuick 2.2

Item {
    Component.onCompleted: {
        var date = new Date("2016-01-30 12:00:00")
        // Prints "30 Jan 2016 12h"
        console.log(Qt.formatDateTime(date, "dd MMM yyyy h'h'"))
    }
}

The Date object is just a standard JS date (with a few QML-specific extensions) and can parse many date strings into a date using the constructor. Read the MDN documentation for Date for more details.

Qt.formatDateTime() is a Qt-specific method used for formatting dates. It excepts a few different standard format types or you can pass your own format, as I did here. Read the Qt.formateDateTime() documentation for more details. You'll also find a table of date format specifiers you can use.

Upvotes: 1

Related Questions