Convert number DDMMYY into date?
I am new to JavaScript (but not programming) and am having a difficult time figuring out where I 开发者_如何学JAVAmade a mistake in this function, found here: http://mikeryan.webatu.com/function.html [dead link][Guessing of the original code at the bottom]
The function should take a DDMMYY timestamp and convert it into a human-readable string. For instance, 210710
would be turned into July 21st, 2010
.
UPDATE: Code that is probably similar OP's dead link:
function timestamp(d){
var year = (d-(Math.round(d / 100)*100);
var day = Math.floor(d/10000);
var dayfix = (day - (Math.floor(day/10)*10));
// var month = ((d-year)-(day*100000)/100);
var a = (d - year);
var b = ((day * 100000) / 10);
var month = (a - b) / 100;
var months = new Array();
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var daysuffix = new Array();
daysuffix[0] = "th";
daysuffix[1] = "st";
daysuffix[2] = "nd";
daysuffix[3] = "rd";
daysuffix[4] = "th";
daysuffix[5] = "th";
daysuffix[6] = "th";
daysuffix[7] = "th";
daysuffix[8] = "th";
daysuffix[9] = "th";
if(year>20){
year = '19' + year;
}
else{
year = '20' + year;
}
return (months[month] + ' ' + day + daysuffix[dayfix] + ', ' + year);
}
One problem: you're missing a parenthesis. Change:
var year = (d-(Math.round(d / 100)*100);
to
var year = (d-(Math.round(d / 100)*100));
That being said, this is a more straightforward calculation method:
var year = d % 100;
var month = Math.floor(d / 100) % 100;
var day = Math.floor(d / 10000) % 100;
Next, your array initialization is unnecessarily verbose. Instead of:
var arr = new Array();
arr[0] = "foo";
arr[1] = "bar";
just do:
var arr = ["foo", "bar"];
Your day suffix is incorrect. It puts "nd" after 12 and "12nd April" clearly isn't correct. I would just use logic for doing this rather than a lookup array where most elements are "th".
So:
function timestamp(d){
var year = d % 100;
var month = Math.floor(d / 100) % 100;
var day = Math.floor(d / 10000) % 100;
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
if (year>20) {
year = '19' + year;
} else {
year = '20' + year;
}
if (day == 1 || day == 21 || day == 31) {
var suffix = "st";
} else if (day == 2 || day == 22) {
var suffix = "nd";
} else {
var suffix = "th";
}
return (months[month-1] + ' ' + day + suffix + ', ' + year);
}
Lastly there is little value in your "timestamp" being an integer in its present form. A more typical format for tis kind of thing is YYYYMMDD for two reasons:
Numerical ordering matches date ordering; and
It's unambiguous. North Americans put month before day (ie MMDDYY). Everyone else in the world puts day first (ie DDMMYY). No one does YYDDMM.
I'd use %
- modulo: X modulo 100 discards anything except the last 2 digits. Useful!
also use floor
not round
Use the date object. It's a whole lot faster. Here is a quick example it assumes the year is going to be in the 2000s so you would have to do some modifications. And the output is not exactly what you have, but it is pretty close and the code is a whole lot shorter.
function date(e){
var d = new Date();
d.setYear(2000+e.substring(4)/1,e.substring(2,4)-1,e.substring(0,2)-1);
alert(d.toDateString());
}
First use Math.floor to get the floor value of the decimal, then there were some typos in your function. Here is the code that works (Note: just tested with a couple of examples) but should be enough to get your started:
function timestamp(d){
var year = (d-(Math.floor(d / 100)*100));
var day = Math.floor(d/10000);
var dayfix = (day - (Math.floor(day/10)*10));
// var month = ((d-year)-(day*100000)/100);
var a = (d - year);
var b = ((day * 100000) / 10);
var month = (a - b) / 100;
var months = new Array();
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var daysuffix = new Array();
daysuffix[0] = "th";
daysuffix[1] = "st";
daysuffix[2] = "nd";
daysuffix[3] = "rd";
daysuffix[4] = "th";
daysuffix[5] = "th";
daysuffix[6] = "th";
daysuffix[7] = "th";
daysuffix[8] = "th";
daysuffix[9] = "th";
if(year>20){
year = '19' + year;
}
else{
year = '20' + year;
}
return (months[month] + ' ' + day + daysuffix[dayfix] + ', ' + year);
}
精彩评论