我正在尝试使用3个提示框来生成当前日期。 3个提示框是全天还是短日? ,整月还是短月?和2位或4位数字的年份?
一旦填写了提示,可能的结果将是(它们在提示框中选择的选项)(当前日期)。
到目前为止,我有这个。 3个提示和带有选择的一些数组
var day = (prompt ("Full day or short day? ", ""));
var month =(prompt("Full month or short month?", ""));
var year =(prompt("2-digit year or 4-digit year?", ""));
var theday = new Date();
var day = theday.getDay();
daylong = [];
daylong[0] = "Sunday";
daylong[1] = "Monday";
daylong[2] = "Tuesday";
daylong[3] = "Wednesday";
daylong[4] = "Thursday";
daylong[5] = "Friday";
daylong[6] = "Saturday";
alert(daylong[5]);
dayshort = [];
dayshort[0] = "Sun";
dayshort[1] = "Mon";
dayshort[2] = "Tue";
dayshort[3] = "Wed";
dayshort[4] = "Thu";
dayshort[5] = "Fri";
dayshort[6] = "Sat";
monthlong = [];
monthlong[0] = "January";
monthlong[1] = "February";
monthlong[2] = "March";
monthlong[3] = "April";
monthlong[4] = "May";
monthlong[5] = "June";
monthlong[6] = "July";
monthlong[7] = "August";
monthlong[8] = "September";
monthlong[9] = "October";
monthlong[10] = "November";
monthlong[11] = "December";
alert(monthlong[8]);
monthshort = [];
monthshort[0] = "Jan";
monthshort[1] = "Feb";
monthshort[2] = "Mar";
monthshort[3] = "Apr";
monthshort[5] = "Jun";
monthshort[6] = "Jul";
monthshort[7] = "Aug";
monthshort[8] = "Sept";
monthshort[9] = "Oct";
monthshort[10] = "Nov";
monthshort[11] = "Dec";
document.write(dayshort[day] + daylong[day]);
答案 0 :(得分:0)
我了解您要执行的操作,但是可能有一种更简单的方法。 moment.js
package是一个非常灵活的程序包,用于处理JavaScript中的Date
个对象。
使用此软件包,取决于警报框中的选择,您可以配置格式表达式,如下所示。
var formats = {
day: {
short: 'ddd',
long: 'dddd'
},
month: {
short: 'MMM',
long: 'MMMM'
},
year: {
short: 'YY',
long: 'YYYY'
}
};
var day_format = prompt('Choose a format for the day. (short or long)') == 'short' ? formats.day.short : formats.day.long;
var month_format = prompt('Choose a format for the month. (short or long)') == 'short' ? formats.month.short : formats.month.long;
var year_format = prompt('Choose a format for the year. (short or long)') == 'short' ? formats.year.short : formats.year.long;
alert('The date is ' + moment().format(month_format + ' ' + day_format + ' ' + year_format));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>