为什么此Javascript脚本不提供ant输出? [英] Why does this Javascript script not give ant output?

查看:47
本文介绍了为什么此Javascript脚本不提供ant输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的实时日期计数器,该脚本将在脚本所在文件的html中输出.这是JSFiddle,这是该脚本:

I want to make a simple live date counter that would output in the html where ever the script is located in the file. Here's the JSFiddle, and here's that script:

var today = newDate();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();

if (dd = 1 || 21 || 31) {
    dd = dd + 'st'
} else if (dd = 2 || 22) {
    dd = dd + 'nd'
} else if (dd = 3 || 23) {
    dd = dd + 'nd'
} else {
    dd = dd + 'th'
}

if (mm = 0) {
    mm = "JANUARY";
} else if (mm = 1) {
    mm = "FEBRUARY";
} else if (mm = 2) {
    mm = "MARCH";
} else if (mm = 3) {
    mm = "APRIL";
} else if (mm = 4) {
    mm = "MAY";
} else if (mm = 5) {
    mm = "JUNE";
} else if (mm = 6) {
    mm = "JULY";
} else if (mm = 7) {
    mm = "AUGUST";
} else if (mm = 8) {
    mm = "SEPTEMBER";
} else if (mm = 9) {
    mm = "OCTOBER";
} else if (mm = 10) {
    mm = "NOVEMBER";
} else {
    mm = "DECEMBER";
}

today = dd + '|' + mm + '|' + yyyy;
document.write(today);

我知道这是编写任何代码的最无效且最不正确的方式(这是我在JS中做过的第二件事.),但请接受我的愚蠢错误.

I know that this is the most inefficint and probably incorrect way of writing any code in general (this is the second thing i've ever done in JS.) but please have some acceptance for my stupid mistakes.

非常感谢.

推荐答案

避免级联if

var month = ["January", "Feb", "March",....,"Dec"];//store array of months as string
var suffix =["st","nd","rd","th"];
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();
var op="";
if(parseInt(dd) > 4)
    op+=dd+""+suffix[3]+"|";
else
    op+=dd+""+suffix[(parseInt(dd)%10)-1]+"|";
op+=month[parseInt(mm)]+"|"+yyyy;

简单易用 工作小提琴

仅供参考:刚才看到了 answer 和0-11几个月而0代表1月,11代表12月

FYI : just now saw the answer and 0-11 for months while 0 represents Jan and 11 represents Dec

这篇关于为什么此Javascript脚本不提供ant输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆