- 浏览: 666413 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (608)
- Java (125)
- Struts (16)
- Spring (19)
- iBatis (11)
- Hibernate (14)
- Oracle (33)
- Mysql (22)
- JBoss (4)
- JavaScript (46)
- Ajax (9)
- jQuery (5)
- web (25)
- Html (14)
- Exception (26)
- Linux (106)
- Vim (11)
- Python (12)
- ComputerTech (25)
- 文章 (50)
- Maven (20)
- FreeMarker (5)
- Json (0)
- Tomcat (6)
- ActiveMQ (2)
- Test (1)
- Git (5)
- Shell (1)
- 《Java并发编程实践》 (2)
- 产品设计 (1)
- xxx (0)
- diamond (1)
- dubbo (1)
- Raspbian (1)
- IDE (3)
- 并发编程 (1)
最新评论
-
mikzhang:
抱歉,写这篇文章时,博主开发的程序只面对IE浏览器,故没考虑其 ...
JS 中改变confirm默认按钮提示“确定""取消" -
poterliu:
测试过了,发现execScript方法只支持IE浏览器。目前已 ...
JS 中改变confirm默认按钮提示“确定""取消"
JavaScript has an inbuilt support for dates and times with its Date object. The methods of the Date object return various values associated with date and time.
To start working with dates and time, we first initialize a variable and assign it a value with the new operator and the Date() constructor. The main function of the new operator withDate() constructor is to create a new date object that is stored in the variable. Here is the code:
var d = new Date();
Thus, variable d contains a new date object. We can now call the various methods of this date object.
Format #1: date-month-year
The three methods that we would be using are:
- getDate(): Returns the date
- getMonth(): Returns the month
- getFullYear(): Returns the year
<script type="text/javascript"> <!-- var d = new Date(); var curr_date = d.getDate(); var curr_month = d.getMonth(); var curr_year = d.getFullYear(); document.write(curr_date + "-" + curr_month + "-" + curr_year); //--> </script>
This prints: 29-7-2011
In the code above, we have a different variable for the day, month and year. These variables all contain numeric quantities. (You can check this using typeof()). Hence, you can perform any kind of numerical operations on these variables.
The eagle-eyed would have noticed that the month value returned by getMonth() is one less than the current month. This is because months start at 0 value, thus, January is represented by 0, February as 1, March as 2 ...
We would have to increment the value returned by getmonth() by 1. The corrected lines of code are:
<script type="text/javascript"> <!-- var d = new Date(); var curr_date = d.getDate(); var curr_month = d.getMonth(); curr_month++; var curr_year = d.getFullYear(); document.write(curr_date + "-" + curr_month + "-" + curr_year); //--> </script>
Prints: 29-8-2011
Format #2: month/day/year
This is very similar to the above:
<script type="text/javascript"> <!-- var d = new Date(); var curr_date = d.getDate(); var curr_month = d.getMonth(); curr_month++; var curr_year = d.getFullYear(); document.write(curr_month + "/" + curr_date + "/" + curr_year); //--> </script>
Prints: 8/29/2011
The getMonth() function gives us the month in a numeric form. To convert this value into the month name, we will employ an array. The array would contain all the 12 month names. Note: For the sake of clarity, I've written the JavaScript code for the array in multiple lines. For usage, you would have to put this on a single line. This time, we use the new operator with the Array() constructor and store the 12 month names in the array. Variable m_names stores the array of month names. The code above prints: 29-August-2011 In this format we include a superscript to the date value. The idea is to identify the date and then select a superscript based on the date value. Note: The last line of code has been split up into two lines. For usage, the entire code should be written on a single line. We first initialize a variable sup that would store the superscript value. Using If-Else-If, we check the value of the current date and accordingly assign a value to sup. The code prints: 29th August 2011 The getDay() method returns a number that specifies the day of the week. Sunday is represented by 0, Monday by 1 and so on. Here again we employ an array. This array would contain the Day names. Note: For the sake of clarity, I've written the code for the arrays and the write() statement have been written on multiple lines. For usage, you would have to put this on a single line. The Day name is accessed from the array using the value returned by getDay() as index. The code above prints: :Monday 29th August 2011 Hours and minutes are obtained using getHours() and getMinutes() methods of the date object. The value returned by gethours() varies from 0 to 23. Print: 9 : 58 There are two problem here. Firstly, if the hour or minutes is in single digits, JavaScript does not add any leading zeroes. Hence, 05:03 (AM) is printed as 5:3. Secondly, 12:00 AM becomes 0:00 (!). Let's see how to format time correctly. The code above is quite simple. First, a variable a_p is initialized. Depending upon the value of curr_hour, we assign AM or PM to this variable. However, we still haven't solved the problem of single digit minutes. When the value returned from getMinutes() is a single digit, we want JavaScript to add a leading zero. The code above prints: 10 : 0 AM To add a leading zero, we first have to check the value returned from getMinutes(). We then convert this numeric value to a string and check its length. If the length is 1, we add a leading zero. JavaScript is a loosely-type language. It means that the data type of a variable can be easily changed. Hence, the same variable storing a number can be made to store a string value. The code above prints: 10 : 00 AM getSeconds() and getMIlliseconds() return the seconds and milliseconds values. Prints: 10:1:38:530 Each of the methods discussed so far calculates time on the client-side (browser). JavaScript also supplies us, similar methods for finding the GMT (called the Universal Coordinated Time or UTC). You can use these methods to find the GMT and format it based on scripts discussed before.Format #3: date-month name-year (something like, 21-March-2001)
<script type="text/javascript">
<!--
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "-" + m_names[curr_month]
+ "-" + curr_year);
/* The last two lines above have
to placed on a single line */
//-->
</script>
The value returned by getMonth() is the index at which the month name is stored in the array. Indexes in JavaScript arrays begin at 0; this suits our purpose and we do not need to increment the getMonth() value.Format #4: Like 21st March 2001
<script type="text/javascript">
<!--
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
{ sup = "st"; }
else if (curr_date == 2 || curr_date == 22)
{ sup = "nd"; }
else if (curr_date == 3 || curr_date == 23)
{ sup = "rd"; }
else
{ sup = "th"; }
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "<SUP>" + sup + "</SUP> "
+ m_names[curr_month] + " " + curr_year);
//-->
</script>
Format #5: day date month name year (something like, Wednesday 21st March 2001)
<script type="text/javascript">
<!--
var d_names = new Array("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday");
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var curr_day = d.getDay();
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
{ sup = "st"; }
else if (curr_date == 2 || curr_date == 22)
{ sup = "nd"; }
else if (curr_date == 3 || curr_date == 23)
{ sup = "rd"; }
else
{ sup = "th"; }
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(d_names[curr_day] + " " + curr_date + "<SUP>"
+ sup + "</SUP> " + m_names[curr_month] + " " + curr_year);
//-->
</script>
Format #6: Hours:Minutes (Finding the time)
<script type="text/javascript">
<!--
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
document.write(curr_hour + " : " + curr_min);
//-->
</script>
Format #7: Correct Time with AM and PM
<script type="text/javascript">
<!--
var a_p = "";
var d = new Date();
var curr_hour = d.getHours();
if (curr_hour < 12)
{ a_p = "AM"; }
else
{ a_p = "PM"; }
if (curr_hour == 0)
{ curr_hour = 12; }
if (curr_hour > 12)
{ curr_hour = curr_hour - 12; }
var curr_min = d.getMinutes();
document.write(curr_hour + " : " + curr_min + " " + a_p);
//-->
</script>
We then change the value of curr_hour. If the value is 0 (as in the case of 12:40 am),curr_hour shall hold the value of 12. Also, if curr_hour is more than 12 we subtract 12 from it to convert the hours to 12 hour format.Format #8: Two digit minutes
<script type="text/javascript">
<!--
var a_p = "";
var d = new Date();
var curr_hour = d.getHours();
if (curr_hour < 12)
{ a_p = "AM"; }
else
{ a_p = "PM"; }
if (curr_hour == 0)
{ curr_hour = 12; }
if (curr_hour > 12)
{ curr_hour = curr_hour - 12; }
var curr_min = d.getMinutes();
curr_min = curr_min + "";
if (curr_min.length == 1)
{ curr_min = "0" + curr_min; }
document.write(curr_hour + " : " + curr_min + " " + a_p);
//-->
</script>
Here we add a blank string to curr_min variable. This operation converts the data type of this variable from number to a string. Using the length property of the string object, we can then check the number of digits. If the number of digits are one, a leading zero is attached to the variable.Format #9: Finding the number of seconds and milliseconds
<script type="text/javascript">
<!--
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var curr_sec = d.getSeconds();
var curr_msec = d.getMilliseconds();
document.write(curr_hour + ":" + curr_min + ":"
+ curr_sec + ":" + curr_msec);
//-->
</script>
Format #10: GMT Time
发表评论
-
javascript_调用父窗口(父页面)的方法
2016-12-23 11:44 635via: http://www.php100.com/htm ... -
防刷新重复提交
2015-11-30 10:41 4061. 现象:在我们系统中,新增、编辑数据后,继续刷新页面后 ... -
Js_“!!”
2014-09-03 15:35 303JS中的“!!” 经常看到这样的例子: var a ... -
Js_==&===
2014-08-24 11:39 608via: http://ilovejsj.iteye.com ... -
Js_数组的操作
2014-07-28 11:10 668via: http://www.cnblogs.com/qi ... -
Js_数组的操作
2014-07-26 14:52 579via: http://www.cnblogs.com/qi ... -
Js_join&split
2014-06-18 16:39 711Js_join 定义和用法 ... -
Js_最常用的55个经典技巧
2014-04-08 17:11 752via: http://www.uml.org.cn/aj ... -
Js_获取当前对象大小及屏幕分辨率等
2014-03-07 10:13 546via: http://www.cnblogs.com/t ... -
Js_表单间数据传递
2013-06-17 10:15 891一,同一个网页里的表单的数据传递。 举个实例,一 ... -
Js_获取控件的绝对位置
2013-04-28 14:55 0from: http://www.cnblogs.co ... -
js_定时关闭网页窗口
2013-04-27 15:38 0<script language="java ... -
js_window.print()
2014-04-15 14:54 843window.print() 功能:打印当前窗口的内容。 ... -
js_URL传参
2013-04-27 14:15 0escape() The escape() func ... -
js_sumbit&onsubmit
2013-04-27 11:35 1191submit与onsubmit 发生顺 ... -
js_输出固定时间格式
2013-04-27 10:47 941/** * 将时间转换成固定格式输出 * new D ... -
js_常识
2013-04-27 10:10 11431.document.write(" " ... -
jQuery_验证电话邮箱
2013-04-27 09:32 1032<HTML> <HEAD> ... -
js_验证电话,邮箱
2013-04-26 16:36 0<HTML> <HEAD> ... -
JS_7个JavaScript简写技巧
2013-04-16 09:25 983from: http://linux.cn/article ...
相关推荐
Publication Date: December 6, 2012 | ISBN-10: 0321812182 | ISBN-13: 978-0321812186 | Edition: 1 “It’s uncommon to have a programming language wonk who can speak in such comfortable and friendly ...
77 Ways to Beat Colds and Flu With sections on understanding colds and flu, tips on prevention and treating, and a look at remedies in the research stage, this handy guide provides all the consumer ...
《More Effective C++: 35 New Ways to Improve Your Programs and Designs》是一本深入探讨C++语言特性的书籍。本书不仅覆盖了C++中较为少见的部分,也重新审视了那些开发者们自认为已经熟悉的领域。通过阅读本书,...
Real-time and embedded systems are essential to our lives, from controlling car engines and regulating traffic lights to monitoring plane takeoffs and landings to providing up-to-the-minute stock ...
Effective C++ Third Edition 55 Specific Ways to Improve Your Programs and Designs By Scott Meyers ............................................... Publisher: Addison Wesley Professional Pub Date: ...
Perkovic's Introduction to Programming Using Python provides an imperative-first introduction to Python focusing on computer applications and the process of developing them. The text helps develop ...
Using Scalable Vector Graphics (SVG) for illustrations only scratches the surface of this format’s potential on the web. With this practical guide, you’ll learn how to use SVG not only for ...
JSON (JavaScript Object Notation) is a lightweight text-based data interchange format used to create objects to transfer data over the Internet. It's widely used today by common web applications, as ...
将toFormat实例方法添加到或 。 安装 节点.js $ npm install toformat 浏览器 < script src =' path/to/big.js ' > </ script > < script src =' path/to/toFormat.js ' > </ script > ...
Get object oriented with this course, which takes you on a journey to get acquainted with few useful hands-on tools, features, and ways to enhance your productivity using OOP techniques. It will also...
Diagnose bad SQL query performance and discover ways to optimize it Grasp modern SQL techniques to optimize complex SQL queries Identify and simplify overly complex JavaScript code Explore and ...
日期和时间的操作在JavaScript中也有其特有的方法,本书中讲解了获取当前日期和时间(Getting the current date and time)、提取日期和时间的各个部分(Extracting parts of the date and time)、指定日期和时间...
10 WAYS TO IMPROVE YOUR PROTECTION WITH MODSECURITY.pdf
- **书名**:《Pragmatic - My Job Went to India, 52 Ways to Save Your Job》 - **作者**:Chad Fowler - **出版社**:The Pragmatic Bookshelf - **出版地点**:Raleigh, North Carolina 和 Dallas, Texas - **...