问题:
[This question is different from other JS Date questions because it focuses on extending the default 3-character limit in the Date function.]
I have created a JavaScript...
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
[This question is different from other JS Date questions because it focuses on extending the default 3-character limit in the Date function.]
I have created a JavaScript new Date
function for HTML5. The Date
function retrieves the current date each time the page is loaded. I need the day of the month (16) before the month (February). I also need the date to display the full length of characters for the date (Saturday, February) instead of the first three characters of the date (Sat, Feb).
In my HTML document, the date currently looks like this:
Sat Feb 16 2019
This is my current JS script:
var d = new Date();
document.getElementById("date").innerHTML = d.toDateString();
I need the date to display all of the characters for the weekday and month.
I need the date to look like this:
Saturday, 16 February 2019
I am still new to JavaScript, so any help or advice is appreciated.
回答1:
You may consider using Moment.js. It’s very useful and simple when working with date formatting.
Otherwise you can use a a function containing switch: case for changing the month’s length and write some other code for changing the places of the day and month.
function dateLongator(mth){
switch:
case “jan”: return “January”;
case “feb”: return “February”;
}
etc.
回答2:
You can use toLocaleDateString
which takes arguments to modify date in specific language conventions
The toLocaleDateString() method returns a string with a language sensitive representation of the date portion of this date. The new locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent. From MDN
var d = new Date();
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
document.getElementById("date").innerHTML = d.toLocaleDateString('en-US', options);
<div id="date"></div>
回答3:
You can use MomentJS
moment().format('dddd, D MMMM YYYY'); // Saturday, 16 February 2019
moment('Sat Feb 16 2019').format('dddd, D MMMM YYYY'); // Saturday, 16 February 2019
回答4:
You can use a function to format the date using the standard Date
methods which will work crossbrowser.
<p></p>
<script>
var d = new Date;
function formatDate(d){
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var day = days[d.getDay()],
month = months[d.getMonth()],
date = d.getDate(),
year = d.getFullYear();
return day + ', ' + date + ' ' + month + ' ' + year;
}
document.querySelector('p').textContent = formatDate(d);
</script>