• Date 对象转换——toString、toTimeString、toDateString、toUTCString、toLocaleString()、toLocaleTimeString()、toLocaleDateString()


    JavaScript toString() 方法

    定义和用法:toString() 方法可把 Date 对象转换为字符串,并返回结果。

    语法:dateObject.toString()

    返回值:dateObject 的字符串表示,使用本地时间表示。

    例子 

    在本例中,我们将把今天的日期转换为字符串:

    <script type="text/javascript">
        var d = new Date()
        document.write (d.toString())
    </script>

    输出:Tue Apr 28 2015 10:05:35 GMT+0800 (中国标准时间)

    JavaScript toTimeString() 方法

    定义和用法:toTimeString() 方法可把 Date 对象的时间部分转换为字符串,并返回结果。

    语法:dateObject.toTimeString()

    返回值:dateObject 的时间部分的字符串表示,由实现决定,使用本地时间表示。

    例子 

    在本例中,我们将根据本地时间把今天的日期转换为字符串:

    <script type="text/javascript">
        var d = new Date()
        document.write(d.toTimeString())
    </script>

    输出:10:05:58 GMT+0800 (中国标准时间)

    JavaScript toDateString() 方法

    定义和用法:toDateString() 方法可把 Date 对象的日期部分转换为字符串,并返回结果。

    语法:dateObject.toDateString()

    返回值:dateObject 的日期部分的字符串表示,由实现决定,使用本地时间表示。

    例子

    在本例中,我们将根据本地时间把今天的日期转换为字符串:

    <script type="text/javascript">
        var d = new Date()
        document.write(d.toDateString())
    </script>

    输出:Tue Apr 28 2015

    JavaScript toUTCString() 方法

    定义和用法:toUTCString() 方法可根据世界时 (UTC) 把 Date 对象转换为字符串,并返回结果。

    语法:dateObject.toUTCString()

    返回值:dateObject 的字符串表示,用世界时表示。

    例子 1

    在下面的例子中,我们将使用 toUTCString() 来把今天的日期转换为(根据 UTC)字符串:

    <script type="text/javascript">
        var d = new Date()
        document.write (d.toUTCString())
    </script>

    输出:Tue, 28 Apr 2015 02:06:45 GMT

    例子 2

    在下面的例子中,我们将把具体的日期转换为(根据 UTC)字符串:

    <script type="text/javascript">
        var born = new Date("July 21, 1983 01:15:00")
        document.write(born.toUTCString())
    </script>

    输出:Wed, 20 Jul 1983 17:15:00 GMT

    JavaScript toLocaleString() 方法

    定义和用法:toLocaleString() 方法可根据本地时间把 Date 对象转换为字符串,并返回结果。

    语法:dateObject.toLocaleString()

    返回值:dateObject 的字符串表示,以本地时间区表示,并根据本地规则格式化。

    例子 1

    在本例中,我们将根据本地时间把今天的日期转换为字符串:

    <script type="text/javascript">
        var d = new Date()
        document.write(d.toLocaleString())
    </script>

    输出:2015/4/28 上午10:07:15

    例子 2

    在本例中,我们将根据本地时间把具体的日期转换为字符串:

    <script type="text/javascript">
        var born = new Date("July 21, 1983 01:15:00")
        document.write(born.toLocaleString())
    </script>

    输出:1983/7/21 上午1:15:00

    JavaScript toLocaleTimeString() 方法

    定义和用法:toLocaleTimeString() 方法可根据本地时间把 Date 对象的时间部分转换为字符串,并返回结果。

    语法:dateObject.toLocaleTimeString()

    返回值:dateObject 的时间部分的字符串表示,以本地时间区表示,并根据本地规则格式化。

    例子

    在本例中,我们将根据本地时间把今天的日期转换为字符串:

    <script type="text/javascript">
        var d = new Date()
        document.write(d.toLocaleTimeString())
    </script>

    输出:上午10:07:35

    JavaScript toLocaleDateString() 方法

    定义和用法:toLocaleDateString() 方法可根据本地时间把 Date 对象的日期部分转换为字符串,并返回结果。

    语法:dateObject.toLocaleDateString()

    返回值:dateObject 的日期部分的字符串表示,以本地时间区表示,并根据本地规则格式化。

    例子 

    在本例中,我们将根据本地时间把今天的日期转换为字符串:

    <script type="text/javascript">
        var d = new Date()
        document.write(d.toLocaleDateString())
    </script>

    输出:2015/4/28

  • 相关阅读:
    网络通信与面向对象:对象化抽象、机制抽象、组合
    类、闭包、monand
    类型是数据的抽象
    投资的本质在控制贪婪
    bootstrap模态框modal使用remote动态加载内容,第二次加载显示相同内容解决办法
    bootstrap模态框关闭后清除模态框的数据
    Ajax发送请求等待时弹出模态框等待提示
    php foreach跳出本次/当前循环与终止循环方法
    PHP二维数组(或任意维数组)转换成一维数组的方法汇总(实用)
    bootstrap模态框动态赋值, ajax异步请求数据后给id为queryInfo的模态框赋值并弹出模态框(JS)
  • 原文地址:https://www.cnblogs.com/sunshq/p/4503437.html
Copyright © 2020-2023  润新知