0

I am trying to change the format of a date from one to another, for exmple 10/05/2012 14:30:56 to 10th May 2012 12:30:56

The javascript function that Ive used is

var monthNames = new Array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");



var today = new Date("10/05/2012 14:30:56");
var cDate = today.getDate();
var cMonth = today.getMonth();
var cYear = today.getFullYear();
var cHour = today.getHours();
var cMin = today.getMinutes();
var cSec = today.getSeconds();
alert( monthNames[cMonth] + " " +cDate  + "," +cYear + " " +cHour+ ":" + cMin+ ":" +cSec );

The output the I get is October 5th which is incorrect. So how do I interchange the date and month?

4

2 に答える 2

2

コンストラクターは、アメリカの日付形式、Dateつまり MM/DD/YYYY を想定しています。英国の日付形式 (DD/MM/YYYY) を使用する場合は、次のように日と月を手動で交換する必要があります。

var date = "10/05/2012 14:30:56";
if (/^(\d+)\/(\d+)(.*)/.test(date))
  date = RegExp.$2 + "/" + RegExp.$1 + RegExp.$3;

var today = new Date(date);

その後、通常の日付変換を実行できます。

于 2012-05-23T08:44:50.857 に答える
0

入力の書式設定と設定のために、ユーザーの日付のデフォルト文字列を知っておくと便利です。セッションごとに 1 回だけ確認する必要があります。

Date.ddmm: (function(){
    return Date.parse('2/6/2009')==1243915200000;
})();

if(Date.ddmm)//format DD/MM/YYYY;
else ////format MM/DD/YYYY;
于 2012-05-23T14:07:49.157 に答える