2015-06-27 19 views
9

Özel biçim tarihlerine ihtiyacım var. Ruby'de, bunu gerçekleştirmek için strftime veya string formatlı zaman kullanırdım.Javascript ile strftime nasıl ayarlanır?

now = Time.new 
now.strftime '%a, %d of %b' #=> "Sat, 27 of Jun" 

Javascript strftime veya eşdeğeri kullanır mı? Javascript'te benzer bir etkiyi nasıl alabilirim?

+0

MomentJs sürüm 2.0 yeni biçimlendirme belirteçleri tanıtıldı ve fo birçok dili destekler rmating [burada belgelenmiştir] (https://gist.github.com/timrwood/e72f2eef320ed9e37c51) –

cevap

9

JavaScript'te tarihleri ​​oluşturmak için yöntemler vardır, ancak biçimlendirilecek yerel kod yoktur. Date() hakkında bilgi edinebilirsiniz. Ancak, libraries var. Özellikle benim için derinlemesine kullanacak en iyi kütüphane MomentJS.

var now = new Date(); 
 

 
document.write(now.toUTCString() + "<br>") 
 
document.write(now.toTimeString() + "<br>")

Date Object bazı özelliklerini: Bir kütüphane kullanmak istemiyorsanız moment().format('dd, d of MMMM')

Ancak, aşağıdaki yerli tarihi özelliklerine erişebilir: Yani olarak böyle bir şey yapabilirsiniz

toDateString() Converts the date portion of a Date object into a readable string 
toGMTString() Deprecated. Use the toUTCString() method instead 
toISOString() Returns the date as a string, using the ISO standard 
toJSON() Returns the date as a string, formatted as a JSON date 
toLocaleDateString() Returns the date portion of a Date object as a string, using locale conventions 
toLocaleTimeString() Returns the time portion of a Date object as a string, using locale conventions 
toLocaleString() Converts a Date object to a string, using locale conventions 
toString() Converts a Date object to a string 
toTimeString() Converts the time portion of a Date object to a string 
toUTCString() Converts a Date object to a string, according to universal time 
İlgili konular