Question
I need to capture the Now() date and time in the YYYY/mm/dd hh:m:sec format. It needs to be in GMT time. How do I achieve that with Javascript?
How-To
You can build it manually:
- test.js
- function getUTCStr(){
- var t = new Date();
- var dateStr = t.getUTCFullYear() + '/' + ("0" + (t.getUTCMonth() + 1)).slice(-2) + '/' + ("0" + (t.getUTCDate() + 1)).slice(-2) + ' ' +
- ("0" + (t.getUTCHours() + 1)).slice(-2) + ':' +
- ("0" + (t.getUTCMinutes() + 1)).slice(-2) + ':' +
- ("0" + (t.getUTCSeconds() + 1)).slice(-2);
- return dateStr
- }
- function getTimeStr(){
- var t = new Date();
- var dateStr = t.getFullYear() + '/' + ("0" + (t.getMonth() + 1)).slice(-2) + '/' + ("0" + (t.getDate() + 1)).slice(-2) + ' ' +
- ("0" + (t.getHours() + 1)).slice(-2) + ':' +
- ("0" + (t.getMinutes() + 1)).slice(-2) + ':' +
- ("0" + (t.getSeconds() + 1)).slice(-2);
- return dateStr
- }
- exports.getUTCStr = getUTCStr
- exports.getTimeStr = getTimeStr
Supplement
* MDN - String.prototype.slice()
* MDN - Date object
沒有留言:
張貼留言