-2

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);

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.

Thanks very much.

Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35

4 Answers4

1

Use

var today = new Date();

instead

var today = newDate();

And output will be "1st|FEBRUARY|2015"


Update: (more elegant way)

b=(new Date()).toLocaleString('en-us', {
  day: 'numeric',
  month: "long",
  year: 'numeric'
}).replace(/(\w+) (\d+), (\d+)/, '$2|$1|$3'); // 30|June|2015
dd=parseInt(b, 10);
op='';
suffix =["st","nd","rd","th"];
if(parseInt(dd) > 4)
    op=dd+""+suffix[3];
else
    op=dd+""+suffix[(parseInt(dd)%10)-1];
alert(b.replace(/^\d+/, op))
Vladimir
  • 342
  • 1
  • 8
  • Today is not the 1st of Feb, is it? – Ja͢ck Jun 30 '15 at 12:40
  • Yep, this was an answer to subject of this thread: "Why does this Javascript script not give ant output?". Btw maybe this will help somehow: (new Date()).toUTCString().replace(/.* (\d+) (\w{3}) (\d{4}).*/, '$1|$2|$3') -> "30|Jun|2015" – Vladimir Jun 30 '15 at 13:17
0

just write: var today = new Date();

Ram G Athreya
  • 4,892
  • 6
  • 25
  • 57
DasDas
  • 571
  • 3
  • 9
  • 32
0

Avoid cascading of if's

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;

MAKE IT SIMPLE working fiddle

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

Community
  • 1
  • 1
theRoot
  • 571
  • 10
  • 33
0

Don't reinvent the wheel, simply use moment.js:

var d = new Date();

document.write(moment().format('Do|MMMM|YYYY'));
<script src="https://raw.githubusercontent.com/moment/moment/develop/moment.js"></script>
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • @theRoot It's 23kB ... and for beginners, I would argue that it's easier so that they can focus on what they want to write. – Ja͢ck Jun 30 '15 at 12:33
  • For beginners to javascript it's highly not recommended – theRoot Jun 30 '15 at 12:38
  • 1
    Let's agree to disagree, then. – Ja͢ck Jun 30 '15 at 12:38
  • Yes I accept if the OP want's to learn JS IMO its not recommended..FYI ..I too newbie in JS...@Ja͢ck – theRoot Jun 30 '15 at 12:40
  • how you are posting answer as comm wiki? – theRoot Jun 30 '15 at 12:46
  • @theRoot There's a checkbox in the answer box ... you won't get any reputation from up-votes, though. – Ja͢ck Jun 30 '15 at 12:47
  • when can I use this? when I don't want to be upvoted?@Ja͢ck – theRoot Jun 30 '15 at 12:50
  • @theRoot Personally I make my answer CW if I vote to close the question; in this case, it's really a bunch of typos that seem to have caused the issues the OP is facing. It doesn't feel right to receive reputation for a question that I want to see closed ;-) – Ja͢ck Jun 30 '15 at 12:52
  • I agree with you, but we guys(startups) tries to solve questions posted by newbies who are little lower than us to get reps :P – theRoot Jun 30 '15 at 13:10