0

Colls、「DD_MM_YYYY_HH_MI_SS」のようなタイムスタンプを返す sToday 変数を作成するコードがあります。

var today = new Date();

var CurrentDay = today.getDay();
var CurrentMonth = today.getMonth();
var CurrentHours = today.getHours();
var CurrentMin = today.getMinutes();
var CurrentSec = today.getSeconds();

if (CurrentDay < 10)
   sToday = "0"+today.getDay().toString();
else 
   sToday = today.getDay().toString();

if(CurrentMonth<10)
  sToday += "_0"+today.getMonth().toString();
else 
  sToday += "_"+today.getMonth().toString();

sToday += "_"+today.getYear().toString();

if (CurrentHours<10)
  sToday += "_0"+today.getHours().toString();
else 
  sToday += "_"+today.getHours().toString();

if (CurrentMin<10)  
  sToday += "_0"+today.getMinutes().toString();
else 
  sToday += "_"+today.getMinutes().toString();

if (CurrentSec<10)
sToday += "_0"+today.getSeconds().toString();
else
sToday += "_"+today.getSeconds().toString();

しかし、13.04.2012 20:20:14 (私の PC 時間) に実行すると、 05_03_2012_20_20_14 を受け取ります。これを修正して 13_04_2012_20_20_14 を受け取る方法は?

4

3 に答える 3

1

.getDay曜日を返します(日曜日は0、月曜日は1、...)。代わりに使用し.getDateます。

function tw(n){
    return (n < 10 ? '0' : '') +  n.toString();
}

var today = new Date();
var sToday = (tw(today.getDate()) + '_' + tw(today.getMonth()+1) + '_' +
             today.getYear().toString() +  '_' + tw(today.getHours()) +
             '_' + tw(today.getMinutes()) + '_' + tw(today.getSeconds()));
于 2012-04-13T16:33:59.513 に答える
0

getDate日付1-31を返します。getDay曜日を0〜6で返し、0は日曜日です。 getMonth0〜11の月を返すため、その値に1を追加する必要があります。

現在、日付、月を印刷する代わりに、日、月-1を印刷しています。

コードを次のように変更します。

if (CurrentDay < 10)
   sToday = "0"+today.getDate().toString();
else 
   sToday = today.getDate().toString();

if(CurrentMonth<10)
  sToday += "_0"+ (today.getMonth() + 1).toString();
else 
  sToday += "_"+(today.getMonth() + 1).toString();
于 2012-04-13T16:35:43.690 に答える
0

日には .getDate を使用し、月に 1 を追加します (月は 0 から始まるため、1 月は 0、2 月は 1 など...)

于 2012-04-13T16:36:42.127 に答える