tags: Javascript
Common 基礎知識
- 基本上沒有處理過的時間的單位為 millisecond
- 資料型態為 long
- JavaScript getTime() Method
- Return the number of milliseconds since 1970/01/01
var d = new Date(); var n = d.getTime();
- The result of n could be:
1514557682972
- 此篇討論的是 endTime - startTime 所得到的時間
Long startTime = resultset.getTimestamp("start_time").getTime() Long endTime = resultset.getTimestamp("end_time").getTime() infoVar.duration = Math.round(((float)(endTime - startTime) / (1000.0)))
- 方法為 三元運算子:
'判斷式' ? 'true的value' : 'false的value'
- 補充:時間是有分英文大小寫的,不可混用
- s:秒, second
- m:分, minute
- h:時, hour
- D:日, Day
- M:月, Month
- Y:年, Year
mm-ss 分-秒
var time = $data.duration;
var minutes = Math.floor(time / 60);
var seconds = time - minutes * 60;
var result = (minutes < 10 ? "0" + minutes : minutes);
result += "-" + (seconds < 10 ? "0" + seconds : seconds);
return result;
結果:02-35
hh-mm-ss 時-分-秒
var time = $data.duration;
var hours = Math.floor(time / 3600);
var minutes = Math.floor((time - hours * 3600) / 60);
var seconds = time - (hours * 3600) - (minutes * 60);
var result = (hours < 10 ? "0" + hours : hours);
result += "-" + (minutes < 10 ? "0" + minutes : minutes);
result += "-" + (seconds < 10 ? "0" + seconds : seconds);
return result;
結果:02-35-36
補充
default 預設
return new Date().toString();
結果:Fri Dec 29 2017 23:29:45 GMT+0800 (台北標準時間)
YYYY-MM-DD 年-月-日
- 比較特別的是 getMonth():The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time. 所以要+1
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth()+1;
var day = d.getDate()
var result = year;
result += "-" + (month < 10 ? "0" + month : month);
result += "-" + (day < 10 ? "0" + day : day);
return result;
結果:2017-08-06
Reference 參考資料
- getTime():https://www.w3schools.com/jsref/jsref_gettime.asp
- seconds to hh mm ss:http://jsfiddle.net/StevenIseki/apg8yx1s
- getMonth():https://www.w3schools.com/jsref/jsref_getmonth.asp
沒有留言:
張貼留言