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
  • 相关阅读:
    libevent
    STL中mem_fun和mem_fun_ref的用法
    java的awt和swing的区别于联系
    数据库流程控制的使用IF CASE LOOP LEAVE ITERETA REPEAT WHILE
    mysql的常用函数
    数据库的基本知识点
    使用myeclipse 打包并运行普通java项目
    getClass()与getName()方法
    Java中的final关键字
    基本类型包装类的常量池技术
  • 原文地址:https://www.cnblogs.com/fromchaos/p/1906414.html
Copyright © 2011-2022 走看看