2017-01-03 4 views
-1

例: この文字列または数字がある場合は020117最初の2つの数字は曜日、最初の4つの数字は曜日と月、フルテキストは曜日、月および年です。入力から日付へのテキストの変換

どうすればいいのですか?020117 - > 02/01/2017

、私は

+1

あなただけのテキストを再フォーマット、またはDateオブジェクトを取得したいですか?どちらの方法でも、2行または3行のコードがこのトリックを行います。何を試しましたか? – RobG

+0

[jsでの文字列の変換]の可能な複製(http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js) – MathSquared

答えて

0
<?php 

$s = "020117"; 
print substr($s, 0, 2)."/".substr($s, 2, 2)."/20".substr($s, 4, 2); 
?> 

または関数として、あなたの助けを必要としてください:

<?php 


function itd($i){ 
return substr($i, 0, 2)."/".substr($i, 2, 2)."/20".substr($i, 4, 2); 
} 

print itd('020117'); 
?> 
0

あなたが好きな簡単な関数で日付に変換することができます:

function parseDMY(s) { 
 
    // Get date parts 
 
    var b = s.match(/\d\d/g); 
 
    var d; 
 
    
 
    // If got 3 parts, convert to Date 
 
    if (b && b.length == 3) { 
 
    d = new Date('20' + b[2], --b[1], b[0]); 
 
    //Check date values were valid, if not set to invalid date 
 
    d = d && d.getMonth() == b[1]? d : new Date(NaN); 
 
    } 
 
    return d;  
 
} 
 

 
// Basic support 
 
console.log(parseDMY('020117').toString()); 
 

 
// New support for toLocaleString 
 
console.log(parseDMY('020117').toLocaleDateString('en-GB'));

それとも文字列を再フォーマット:

var s = '020117'; 
 
console.log(s.replace(/(\d\d)(\d\d)(\d\d)/, '$1/$2/20$3'))

関連する問題