I am sure there is a simply solution but I am relatively new at this. I have the following code that works fine except I need help reformatting the date:
Code:
<script>
var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
document.write("Information is for week ending "+ lastday)
</script>
It returns the following:
Information is for week ending Sat, 2 Feb 2013 15:59:49 UTC
I would like it it to read:
Information is for week ending 02/02/2013
<script>
Number.prototype.padDigit = function() { return (this < 10) ? '0'+this : this; }
var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first));
var lastday = new Date(curr.setDate(last));
var lday = (lastday.getMonth()+1).padDigit()
+ '/'+lastday.getDate().padDigit()
+ '/'+(lastday.getFullYear()-2000).padDigit();
document.write("Information is for week ending "+ lday)
</script>
Remove the -2000 in the FullYear() caluclation for 4 digit year.
Good Luck!
Last edited by jmrker; 01-29-2013 at 04:33 PM..
Reason: Finally typed faster than 'Philip M'. Yeah !!!
<script type = "text/javascript">
var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var lastday = new Date(curr.setDate(last));
var yy = lastday.getFullYear();
var mth = lastday.getMonth()+1;
if (mth<10) {mth = "0" + mth}
var dy = lastday.getDate();
if (dy<10) {dy = "0" + dy}
// now manipulate the display of year,month,date as desired
var result = dy + "/" + mth + "/" + yy; // UK format - change for USA format if required
document.write("Information is for week ending "+ result)
</script>
Be aware that document.write() has been obsolete since Netscape 3 passed away 10+ years ago. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.
The people who followed the Lord were called the 12 Decibels.
- Pupil's answer to Catholic Elementary School test.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
String.prototype.times = function(n)
{
var s = '';
for (var i = 0; i < n; i++)
s += this;
return s;
}
// Zero-Padding
String.prototype.zp = function(n) { return '0'.times(n - this.length) + this; }
// a global month names array
var gsMonthNames = new Array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);
// a global day names array
var gsDayNames = new Array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
);
// the date format prototype
Date.prototype.format = function(f)
{
if (!this.valueOf())
return ' ';
var d = this;
return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi,
function($1)
{
switch ($1.toLowerCase())
{
case 'yyyy': return d.getFullYear();
case 'mmmm': return gsMonthNames[d.getMonth()];
case 'mmm': return gsMonthNames[d.getMonth()].substr(0, 3);
case 'mm': return (d.getMonth() + 1).toString().zp(2);
case 'dddd': return gsDayNames[d.getDay()];
case 'ddd': return gsDayNames[d.getDay()].substr(0, 3);
case 'dd': return d.getDate().toString().zp(2);
case 'hh': return ((h = d.getHours() % 12) ? h : 12).zp(2);
case 'nn': return d.getMinutes().toString().zp(2);
case 'ss': return d.getSeconds().toString().zp(2);
case 'a/p': return d.getHours() < 12 ? 'a' : 'p';
}
}
);
}
var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = (new Date(curr.setDate(first))).format('dd/mm/yyyy');
var lastday = (new Date(curr.setDate(last))).format('dd/mm/yyyy');
document.write("Information is for week ending "+ lastday);
Seems to me, if you are going to go to this much trouble, you might as well cover all the bases.
You mean like being able to display the date as a julian day number or the time in the decimal format that swatch invented or with a suffix indicating whether it is daylight saving time or not - or even display the date as a calendar for the month with the day highlighted.
Really nice. But I have a ton of suggestions. <grin/>
You really want them?
I'd like to see stuff such as
Code:
var diff = d1.difference( d2, Date.MINUTE );
I don't see how it's possible to add "static" date members such as Date.MINUTE to a prototype, so if it's not, I'd settler for something simpler. Such as:
Code:
var DATE = {
SUNDAY : 0,
MONDAY : 1,
...
SATURDAY : 6,
JANUARY : 0,
...
DECEMBER : 11,
SECOND : 1000,
MINUTE : 60000,
...
};
(Not fixed at all on the values for SECOND, MINUTE, etc. Might be cleaner to use characters or negative numbers for their values. Implementation not important.)
And then we would do:
Code:
var diff = d1.difference( d2, DATE.MINUTE ); // DATE, not Date
Question: Does your toCalendar( ) routine provide the ability to "pick" dates and get them back in particular string formats? Notably DD/MM/YYYY, MM/DD/YYYY, and YYYY/MM/DD? Or does it just produce a Date() object and then one uses your format method to produce what is desired?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Really nice. But I have a ton of suggestions. <grin/>
You really want them?
I am always open to suggestions - that's the way that scripts gradually get improved.
Quote:
Originally Posted by Old Pedant
I'd like to see stuff such as
Code:
var diff = d1.difference( d2, Date.MINUTE );
I don't see how it's possible to add "static" date members such as Date.MINUTE to a prototype, so if it's not, I'd settler for something simpler. Such as:
Code:
var DATE = {
SUNDAY : 0,
MONDAY : 1,
...
SATURDAY : 6,
JANUARY : 0,
...
DECEMBER : 11,
SECOND : 1000,
MINUTE : 60000,
...
};
(Not fixed at all on the values for SECOND, MINUTE, etc. Might be cleaner to use characters or negative numbers for their values. Implementation not important.)
And then we would do:
Code:
var diff = d1.difference( d2, DATE.MINUTE ); // DATE, not Date
I'll have to think about that one. The problem with the Date object in JavaScript is that it isn't implemented as a proper object and so you can't use inheritance with it at all - I had to find an alternate way to create an extended object. So implementing static data members would work a bit differently and might be easier to actually implement using the prototype.
Quote:
Originally Posted by Old Pedant
Question: Does your toCalendar( ) routine provide the ability to "pick" dates and get them back in particular string formats? Notably DD/MM/YYYY, MM/DD/YYYY, and YYYY/MM/DD? Or does it just produce a Date() object and then one uses your format method to produce what is desired?
Not yet - it is on my list of things to add in the near future as I have several scripts that will be able to use that calendar code once it has that functionality.
Great. I really like the idea of being able to use "constants" of the form DATE.JANUARY (or whatever the prefix needs to be) as well as having "generic" functions for things like
Code:
var d2 = d1.dateAdd( 30, DATE.MINUTE );
var seconds = d1.dateDiff( d2, DATE.SECOND );
and so on
You probably recognize the direction I'm going from MySQL:
Code:
date_add( somedate, INTERVAL 30 MINUTE );
and so on.
Hmmm...I guess we could opt for INTERVAL.MINUTE but what doesn't for for JANUARY and so on.
Keep at it! I'll peek in from time to time. Thanks!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Question: Does your toCalendar( ) routine provide the ability to "pick" dates and get them back in particular string formats? Notably DD/MM/YYYY, MM/DD/YYYY, and YYYY/MM/DD? Or does it just produce a Date() object and then one uses your format method to produce what is desired?
New answer as I just changed it.
Now if you pass a function as a parameter to the toCalendar() call then clicking on a date in the calendar will call that function passing the year, month and day that was clicked. In that function you could then use those values to format the date however you like and put it where ever you like.
Still thinking about how to best implement the constants - I'm tempted toward using the prototype as my extended date object doesn't use that for anything yet.