QML学习笔记(二)-获取当前系统时间

QML学习笔记(二)-获取当前系统时间

1.方法1

import QtQuick 2.0
Timer {
            interval: 500;
            running: true;
            repeat: true;
            onTriggered: time.text = Qt.formatDateTime(new Date(), "dddd\nyyyy-MM-dd\n-MMM-\nhh-mm-ss")
                                                                  // 星期 年份 月份 号 大月份
        }

        Text {
            id: time;
            color: "#fff";
            font.pointSize: 14;
            anchors.centerIn: parent;
        }

1.方法2

import QtQuick 2.0
Rectangle{


    Timer {
            interval: 500; running: true; repeat: true
            onTriggered: time.text = getCurDate().toString();

        }
        function getCurDate()
        {
             var d = new Date();
             var week;
             switch (d.getDay()){
             case 1: week="星期一"; break;
             case 2: week="星期二"; break;
             case 3: week="星期三"; break;
             case 4: week="星期四"; break;
             case 5: week="星期五"; break;
             case 6: week="星期六"; break;
             default: week="星期天";
             }
             var years = d.getFullYear();
             var month = add_zero(d.getMonth()+1);
             var days = add_zero(d.getDate());
             var hours = add_zero(d.getHours());
             var minutes = add_zero(d.getMinutes());
             var seconds=add_zero(d.getSeconds());
             var ndate = years+"-"+month+"-"+days+"\n"+hours+":"+minutes+":"+seconds;
             return ndate;
        }

        function add_zero(temp)
        {
             if(temp<10) return "0"+temp;
             else return temp;
        }


        Text {
            id: time;
            color: "#fff";
            font.pointSize: 14;
            anchors.centerIn: parent;
        }

}

 

 参考:http://blog.chinaunix.net/uid-26663150-id-3308107.html

 

上一篇:12因子应用


下一篇:译: 进化的系统需要进化的系统工程