最简单的JS给日期补0

JavaScript09

最简单的JS给日期补0,第1张

适合用在已经有取到没有格式化的数据了

//获取系统当前时间

var nowdate = new Date()

var  y = nowdate.getFullYear()

var m = (nowdate.getMonth()+1).toString().padStart(2,'0')

var d = nowdate.getDate().toString().padStart(2,'0')

var currentTime = y+'-'+m+'-'+d

that.date1=currentTime

that.date3=currentTime

//获取系统前一个月的时间

  nowdate.setMonth(nowdate.getMonth()-1)

  var  y = nowdate.getFullYear()

  var m = (nowdate.getMonth()+1).toString().padStart(2,'0')

  var d = nowdate.getDate().toString().padStart(2,'0')

  var preMonth = y+'-'+m+'-'+d

  that.date=preMonth

  that.date2=preMonth

适用于时间戳转换日期的时候顺便补0

var change = new Date(that.dataList[i][0] * 1000)

var Y = change.getFullYear() + "-"

var M = (change.getMonth() + 1 <10 ? "0" + (change.getMonth() + 1) : change.getMonth() + 1) + "-"

var D= (change.getDate() <10 ? "0" + (change.getDate()) : change.getDate()) + " "

var h= (change.getHours() <10 ? "0" + (change.getHours()) : change.getHours()) + ":"

var m= (change.getMinutes() <10 ? "0" + (change.getMinutes()) : change.getMinutes()) + ":"

var s= (change.getSeconds() <10 ? "0" + (change.getSeconds()) : change.getSeconds())

var newDate = Y + M + D + h + m + s

加日期方法:

//beginDate 需要参加计算的日期,days要添加的天数,返回新的日期,日期格式:YYYY-MM-DD。

function getDay(beginDate, days) 。

var beginDate = beginDate.split("-")。

var nDate = new Date(beginDate[1]+ '-' + beginDate[2]+ '-' + beginDate[0])//转换为

MM-DD-YYYY格式。

var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000)。

var rDate = new Date(millSeconds)。

var year = rDate.getFullYear()。 var month = rDate.getMonth() + 1。

if (month <10) month = "0" + month。

var date = rDate.getDate()。

if (date <10) date = "0" + date。

return (year + "-" + month + "-" + date)。

1.绑定时格式化日期方法:

<ASP:BOUNDCOLUMN    DATAFIELD=    "JoinTime    "    DATAFORMATSTRING=

"{0:yyyy-MM-dd}    "    >  <ITEMSTYLE    WIDTH=    "18%    "    >

</ITEMSTYLE    >      </ASP:BOUNDCOLUMN    >。

2.数据控件如DataGrid/DataList等的件格式化日期方法:

e.Item.Cell[0].Text=Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString()。

3.用String类转换日期显示格式:

String.Format(    "yyyy-MM-dd    ",yourDateTime。