0

I'm using a script to display the current date to someone, but I want to do two things to it.

First, I want to change the format to MM/DD/YY, so it's only showing the year in 2 digits.

Then second, how can I add a default? So if it's not able to be pulled by someone, it will show "Today" instead of a date?

Here's the script if anyone could help:

 var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
king
  • 1,304
  • 3
  • 23
  • 43
  • try this Link http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript – Adam May 20 '13 at 05:25

3 Answers3

2

I don't see any reason for this to fail, If javascript is enabled in the client it should work, still if you want error handling

try {
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write(month + "/" + day + "/" + (year + '').substring(2))
}catch(e){
    document.write('Today');
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • what is the catch(e) ? i am putting this directly into my html wrapped in a – king May 20 '13 at 05:30
  • im thinking it might fail because it will be for people with cell phones so im not sure 100% if it will work for them – king May 20 '13 at 05:31
  • @sab if javascript is available in the mobile browser it should work, the `try...catch` is an error handling mechanism – Arun P Johny May 20 '13 at 05:34
  • thanks aron. i tried to add catch(e) underneath the .substring(2)), however, I got an error on the catch(e) line. is there someway to seperate this or what is the proper context when i'm putting this directly into the html using ? – king May 20 '13 at 05:37
2

Working FIDDLE Demo

Add a div to your HTML and put your default text inside it:

<div id="date">Today</div>

Then with javascript, change it, and if the user don't have javascript, it remains not changed:

// create date
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = month + "/" + day + "/" + (year + '').substring(2);

// now insert it
document.getElementById('date').innerHTML = date;
  • [!] Don't forget to add the JS after your element or add it to document ready.
  • what should i call the js file? i treid to do this and called the JS file date.js, but it didn't show anything. don't i have to add write somewhere within a – king May 20 '13 at 05:44
  • i added to my header.. but still this is not working.. – king May 20 '13 at 05:53
  • 1
    Write them in a file like `date.js` and put `` before `

    ` tag.

    –  May 20 '13 at 05:53
  • 1
    @sab You must add it before the `

    ` tag to ensure that your `id="date"` element is exists.

    –  May 20 '13 at 05:56
0

You can get the last two digits of day by doing String(year).substring(2,4). So you end up with:

console.log(day+"/"+month+"/"+String(year).substring(2,4));

To set defaults you can use the following:

var demo = value1 || default

You can play around with these ideas here

HennyH
  • 7,794
  • 2
  • 29
  • 39