zoukankan      html  css  js  c++  java
  • JavaScript Date Format

     

    Although JavaScript provides a bunch of methods for getting and setting parts of a date object, it lacks a simple way to format dates and times according to a user-specified mask. There are a few scripts out there which provide this functionality, but I've never seen one that worked well for me… Most are needlessly bulky or slow, tie in unrelated functionality, use complicated mask syntaxes that more or less require you to read the documentation every time you want to use them, or don't account for special cases like escaping mask characters within the generated string.

    When choosing which special mask characters to use for my JavaScript date formatter, I looked at PHP's date function and ColdFusion's discrete dateFormat and timeFormat functions. PHP uses a crazy mix of letters (to me at least, since I'm not a PHP programmer) to represent various date entities, and while I'll probably never memorize the full list, it does offer the advantages that you can apply both date and time formatting with one function, and that none of the special characters overlap (unlike ColdFusion where m and mm mean different things depending on whether you're dealing with dates or times). On the other hand, ColdFusion uses very easy to remember special characters for masks.

    With my date formatter, I've tried to take the best features from both, and add some sugar of my own. It did end up a lot like the ColdFusion implementation though, since I've primarily used CF's mask syntax.

    Before getting into further details, here are some examples of how this script can be used:

    Following are the special characters supported. Any differences in meaning from ColdFusion's dateFormat and timeFormat functions are noted.


    A couple issues:

    • In the unlikely event that there is ambiguity in the meaning of your mask (e.g., m followed by mm, with no separating characters), put a pair of empty quotes between your metasequences. The quotes will be removed automatically.
    • If you need to include literal quotes in your mask, the following rules apply:
      • Unpaired quotes do not need special handling.
      • To include literal quotes inside masks which contain any other quote marks of the same type, you need to enclose them with the alternative quote type (i.e., double quotes for single quotes, and vice versa). E.g., date.format('h "o\'clock, y\'all!"') returns "6 o'clock, y'all". This can get a little hairy, perhaps, but I doubt people will really run into it that often. The previous example can also be written as date.format("h") + "o'clock, y'all!".

    Here's the code:


    转载:http://blog.stevenlevithan.com/archives/date-time-format
  • 相关阅读:
    RPC之Thrift系列1-----Thrit介绍
    MYSQL-实现sqlserver- row_number() over(partition by order by) 分组排序功能
    Sql Server 中 PIVOT在mysql 中的实现
    MSSQL中 ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)在Mysql中的实现
    mysql判断表中符合条件的记录是否存在
    mysql对树进行递归查询
    MySql5.6中的表按照时间进行表分区过程中遇到的坑
    .net平台上实现数据库访问工厂,连接不同的数据库
    VS静态编译与动态编译
    CRC原理阐述
  • 原文地址:https://www.cnblogs.com/fromchaos/p/1906414.html
Copyright © 2011-2022 走看看