前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js中将日期类型转换成字符串型格式

js中将日期类型转换成字符串型格式

作者头像
源哥
发布2018-08-28 11:29:21
3.3K0
发布2018-08-28 11:29:21
举报
文章被收录于专栏:源哥的专栏源哥的专栏
代码语言:javascript
复制
<script language="javascript">
/////////////////////////////////e///////////////////////
// 取得当前日期,格式yyyy-mm-dd
////////////////////////////////////////////////////////
function GetCurrentDate()
{
    var Year=0;
    var Month=0;
    var Day=0;
    var CurrentDate = new Date();
    return ChangeDateToString(CurrentDate);
}

/////////////////////////////////e///////////////////////
// 取得当前日期,格式yyyy-mm-dd hh:mm
////////////////////////////////////////////////////////
function GetCurrentTime()
{
    var Year=0;
    var Month=0;
    var Day=0;
    var CurrentDate = new Date();
    return ChangeTimeToString(CurrentDate);
}
////////////////////////////////////////////////////////
// 将日期类型转换成字符串型格式yyyy-MM-dd 
////////////////////////////////////////////////////////
function ChangeDateToString(DateIn)
{
    var Year=0;
    var Month=0;
    var Day=0;
    var CurrentDate="";
    //初始化时间
    Year      = DateIn.getYear();
    Month     = DateIn.getMonth()+1;
    Day       = DateIn.getDate();

    CurrentDate = Year + "-";
    if (Month >= 10 )
    {
        CurrentDate = CurrentDate + Month + "-";
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Month + "-";
    }
    if (Day >= 10 )
    {
        CurrentDate = CurrentDate + Day ;
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Day ;
    }
 
    return CurrentDate;
}
///////////////////////////////////////////////////////
// 将日期类型转换成字符串型格式yyyy-MM-dd hh:mm
////////////////////////////////////////////////////////
function ChangeTimeToString(DateIn)
{
    var Year=0;
    var Month=0;
    var Day=0;
    var Hour = 0;
    var Minute = 0;
    var CurrentDate="";
    //初始化时间
    Year      = DateIn.getYear();
    Month     = DateIn.getMonth()+1;
    Day       = DateIn.getDate();
    Hour      = DateIn.getHours();
    Minute    = DateIn.getMinutes();
 
    CurrentDate = Year + "-";
    if (Month >= 10 )
    {
        CurrentDate = CurrentDate + Month + "-";
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Month + "-";
    }
    if (Day >= 10 )
    {
        CurrentDate = CurrentDate + Day ;
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Day ;
    }
 
     if(Hour >=10)
    {
        CurrentDate = CurrentDate + " " + Hour ;
    }
    else
    {
        CurrentDate = CurrentDate + " 0" + Hour ;
    }
    if(Minute >=10)
    {
        CurrentDate = CurrentDate + ":" + Minute ;
    }
    else
    {
        CurrentDate = CurrentDate + ":0" + Minute ;
    }      
    return CurrentDate;
}
</script>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2005年03月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档