zoukankan      html  css  js  c++  java
  • js 时间格式化

    开发的过程中经常会碰到时间格式化的事,针对那些时间戳,2015-05-05,2015/05/05等都能很好的转换成你想要的格式

    function FormatDate(strDate, strFormat)
    {
      if (!strDate) return;
      if (!strFormat) format = "yyyy-MM-dd";
      switch (typeof strDate)
      {
        case "string":
          strDate = new Date(strDate.replace(/-/g, "/"));
        break;
        case "number":
          strDate = new Date(strDate);
        break;
      }
      if (!strDate instanceof Date) return;
      var dict = {
        "yyyy": strDate.getFullYear(),
        "M": strDate.getMonth() + 1,
        "d": strDate.getDate(),
        "H": strDate.getHours(),
        "m": strDate.getMinutes(),
        "s": strDate.getSeconds(),
        "MM": ("" + (strDate.getMonth() + 101)).substr(1),
        "dd": ("" + (strDate.getDate() + 100)).substr(1),
        "HH": ("" + (strDate.getHours() + 100)).substr(1),
        "mm": ("" + (strDate.getMinutes() + 100)).substr(1),
        "ss": ("" + (strDate.getSeconds() + 100)).substr(1)
      };
      return strFormat.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function ()
      {
        return dict[arguments[0]];
      });
    }

    FormatDate(strDate, "yyyy-MM-dd HH:mm:ss")

  • 相关阅读:
    ASP.NET和PHP全面对比
    GridView事件DataBinding,DataBound,RowCreated,RowDataBound区别及执行顺序分析
    OA、CRM、ERP之间的区别和联系是什么?
    C#继承
    对软件项目管理的几点认识
    冒泡
    经典排序算法
    asp.net遍历页面中所有TextBox,并赋值为String.Empty的方法
    String.Format用法
    frame和iframe
  • 原文地址:https://www.cnblogs.com/ghelement/p/4511373.html
Copyright © 2011-2022 走看看