javascriptで日時を取得する処理
掲題の通りです。お好みのフォーマットにするのは意外とめんどくさい・・。
//日時を生成して返却する関数 //「2015-04-18 18:01:07」みたいなフォーマットで返却 function get_date_str(){ var now = new Date(); var year = now.getFullYear(); var month = now.getMonth() + 1; var day = now.getDate(); var hour = now.getHours(); var min = now.getMinutes(); var sec = now.getSeconds(); month = ('0' + month).slice(-2); day = ('0' + day).slice(-2); hour = ('0' + hour).slice(-2); min = ('0' + min).slice(-2); sec = ('0' + sec).slice(-2); var str = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec; return str; } var now = get_date_str(); console.log(now);