Formatting Money (9,999.00)
function FormatCurrency (num) {
num = num.tostring (). Replace (/\$|\,/g, ");
if (IsNaN (num))
num = "0";
Sign = (num = = (num) = Math.Abs (num));
num = Math.floor (num * 100 + 0.50000000001);
cents = num% 100;
num = Math.floor (num/100). toString ();
if (cents < 10)
cents = "0" + cents;
for (var i = 0; i < Math.floor (Num.length-(1 + i)/3); i++)
num = num.substring (0, Num.length-(4 * i + 3)) + ', ' +
Num.substring (Num.length-(4 * i + 3));
Return ((sign)? ': '-') + num + '. ' + cents);
}
Format the amount in the HTML page
In the Span tab, for example.
<span> <script>var Money =formatcurrency (999999.0); document.write (money) </script> </span>
Output Content 999,999.00
Formatted date
Formatted Date FMT is a formatted format
Date.prototype.pattern = function (FMT) {
var o = {
"m+": This.getmonth () + 1,//month
"D+": this.getdate (),//day
"H +": this.gethours ()% 12 = = 0? 12:this.gethours ()% 12,//hour
"H +": this.gethours (),//hour
"m+": this.getminutes (),//min
"S+": This.getseconds (),//sec
"q+": Math.floor ((This.getmonth () + 3)/3),//Quarterly
"S": this.getmilliseconds ()//MS
};
var week = {
"0": "/u65e5",
"1": "/u4e00",
"2": "/u4e8c",
"3": "/u4e09",
"4": "/u56db",
"5": "/u4e94",
"6": "/U516D"
};
if (/(y+)/.test (FMT)) {
FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));
}
if (/(e+)/.test (FMT)) {
FMT = Fmt.replace (regexp.$1, (Regexp.$1.length > 1)? (Regexp.$1.length > 2?) "/u661f/u671f": "/u5468"): "") + week[this.getday () + ""]);
}
For (var k in O) {
if (New RegExp ("(" + K + ")"). Test (FMT)) {
FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr (("" + O[k]).));
}
}
return FMT;
}
<span><script>var date = new Date (Date.parse (' $opr. Orderlist.ol_ordertime '. Replace (/-/g, "/"))); document.write (Date.pattern ("yyyy mm month DD Day");</span>
Formatted date
//An extension to date that converts date to a string of the specified format//the Month (m), Day (d), hour (h), Minute (m), second (s), quarter (q) can be used with 1-2 placeholders,//year (Y) can use 1-4 placeholders, milliseconds (S) with only 1 placeholders (1-3 digits)//Example://(New Date ()). Format ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423//(New Date ()). Format ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18Date.prototype.Format =function(FMT) {//Author:meizz varo = { "m+": This. GetMonth () + 1,//Month"D+": This. GetDate (),//Day"H +": This. GetHours (),//hours"m+": This. getminutes (),//points"S+": This. getseconds (),//seconds"q+": Math.floor (( This. GetMonth () + 3)/3),//Quarterly"S": This. Getmilliseconds ()//milliseconds }; if(/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); for(varKincho)if(NewRegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr ("" +o[k])); returnFMT;} Call:varTime1 =NewDate (). Format ("Yyyy-mm-dd");varTime2 =NewDate (). Format ("Yyyy-mm-dd HH:mm:ss");<script language= "javascript" type= "Text/javascript" > <!--/** * For the extension of date, the date is converted to a specified format of string * months (M), Day (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Weeks (E), quarter (q) can be used with 1-2 placeholders * year (y) can use 1-4 placeholders , milliseconds (S) can only be used with 1 placeholders (1-3 digits) * Eg: * (new Date ()). Pattern ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423 * (New Date ()). Pattern (" Yyyy-mm-dd E HH:mm:ss ") ==> 2009-03-10 II 20:09:04 * (new Date ()). Pattern ("Yyyy-mm-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 * (new Date ()). Pattern ("Yyyy-mm-dd E EE hh:mm:ss ") ==> 2009-03-10 Tuesday 08:09:04 * (New Date ()). Pattern (" yyyy-m-d h:m:s.s ") ==> 2006-7-2 8:9:4.18 */Date.prototype.pattern=function(FMT) {varo = { "m+": This. GetMonth () +1,//Month"D+": This. GetDate (),//Day"H +": This. getHours ()%12 = = 0? 12: This. GetHours ()%12,//hours"H +": This. GetHours (),//hours"m+": This. getminutes (),//points"S+": This. getseconds (),//seconds"q+": Math.floor (( This. GetMonth () +3)/3),//Quarterly "S": This. Getmilliseconds ()//milliseconds }; varWeek = { "0": "/u65e5", "1": "/u4e00", "2": "/u4e8c", "3": "/u4e09", "4": "/u56db", "5": "/u4e94", "6": "/U516D" }; if(/(y+)/. Test (FMT)) {FMT=fmt.replace (Regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); } if(/(e+)/. Test (FMT)) {FMT=fmt.replace (regexp.$1, (regexp.$1.length>1)? (regexp.$1.length>2?) "/u661f/u671f": "/u5468"): "") +week[ This. GetDay () + ""]); } for(varKincho) { if(NewRegExp ("(" + K + ")"). Test (FMT)) {FMT= Fmt.replace (regexp.$1, (regexp.$1.length==1)? (O[k]): (("XX" + o[k]). substr ("" +o[k])); } } returnFMT; } varDate =NewDate (); Window.alert (Date.pattern ("Yyyy-mm-dd Hh:mm:ss"));// -</script>Date.prototype.format=function(mask) {varD = This; varZeroize =function(value, length) {if(!length) Length = 2; Value=String (value); for(vari = 0, zeros = "; I < (length-value.length); i++) {Zeros+ = ' 0 '; } returnZeros +value; }; returnMask.replace (/"[^"]* "|" [^ ']* ' |/b (?: D {1, 4 } |m {1, 4 } | YY (?: yy)? | ([HHMSTT])/1? | [LLZ]) /b/g,function($) { Switch($) { Case' d ': returnd.getdate (); Case' DD ': returnzeroize (D.getdate ()); Case' DDD ': return[' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thr ', ' Fri ', ' Sat '][d.getday ()]; Case' dddd ': return[' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '][d.getday ()]; CaseM: returnD.getmonth () + 1; Case' MM ': returnZeroize (D.getmonth () + 1); Case' MMM ': return[' Jan ', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' Jul ', ' April ', ' Sep ', ' Oct ', ' Nov ', ' Dec '][d.getmonth ()]; Case' MMMM ': return[' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' November ', ' December '][d.getmonth ()]; Case' YY ': returnString (D.getfullyear ()). substr (2); Case' YYYY ': returnd.getfullyear (); Case' H ': returnD.gethours ()% 12 | | 12; Case' HH ': returnZeroize (d.gethours ()% 12 | | 12); CaseH: returnd.gethours (); Case' HH ': returnzeroize (D.gethours ()); Case' m ': returnd.getminutes (); Case' mm ': returnzeroize (D.getminutes ()); Case' s ': returnd.getseconds (); Case' SS ': returnzeroize (D.getseconds ()); CaseL: returnZeroize (D.getmilliseconds (), 3); CaseL: varm =d.getmilliseconds (); if(M > mm) m = Math.Round (M/10); returnzeroize (m); Case' TT ': returnD.gethours () < 12? ' Am ': ' PM '; Case' TT ': returnD.gethours () < 12? ' AM ': ' PM '; CaseZ: returnD.toutcstring (). Match (/[a-z]+$/); //Return quoted strings with the surrounding quotes removed default: return$0.SUBSTR (1, $0.length-2); } });};
View Code
Formatted Date reference http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html
Formatting money and dates in nvelocity