How to format date in javascript

{0 Comments}

The JavaScript have inbuilt date class which can be used to stored and get date and time. To initialize its object we use following syntax.

Var a =new date();

Above syntax a object of date class with value of current date and time.  Then you can use following functions to get each part like date, month, year which you can use to format a date in appropriate format.

  • Getdate();
  • Getmonth();
  • Getyear();

Following is the example how you can format current date time in dd-mm-yyyy formats.

 Javascript |  copy code |? 
01
<script type="text/javascript">
02
<!--
03
 
04
var d = new Date();
05
 
06
var currdate = d.getDate();
07
 
08
var currmonth = d.getMonth();
09
 
10
var curryear = d.getFullYear();
11
 
12
document.write(currdate + "-" + currmonth + "-" + curryear);
13
 
14
//-->
15
</script>

  To do more detailed formatting in different manner you can use following inbuilt functions.

getFullYear ()
getMonth()
getDate()
getDay()
getHours()
getMinutes()
getSeconds()
getMilliseconds()
getTimezoneOffset()
getUTCFullYear ()
getUTCMonth()
getUTCDate()
getUTCDay()
getUTCHours()
getUTCMinutes()
getUTCSeconds()
getUTCMilliseconds()
setFullYear()
setMonth()
setDate()
setHours()
setMinutes()
setSeconds()
setMilliseconds()
setUTCFullYear()
setUTCMonth()
setUTCDate()
setUTCHours()
setUTCMinutes()
setUTCSeconds()
setUTCMilliseconds()
parse()
UTC(y, m, d, h, m, s)
toString()
toUTCString()
toLocaleString()
toDateString()
toTimeString()

Leave a Comment

Your email address will not be published.