Javascript gets and formats the current date

Possible duplicate:
JavaScript date formatting

I need to show the current date in a format like this (some examples):

sep 10, 2012 nov 5, 2012 

etc.

Using javascript I get the current date object

 var date = new Date(); 

what do i need to do next?

+4
source share
5 answers

you can use this.

 function dateTest(){ var d =new Date(); var month_name=new Array(12); month_name[0]="Jan" month_name[1]="Feb" month_name[2]="Mar" month_name[3]="Apr" month_name[4]="May" month_name[5]="Jun" month_name[6]="Jul" month_name[7]="Aug" month_name[8]="Sep" month_name[9]="Oct" month_name[10]="Nov" month_name[11]="Dec" alert(month_name[d.getMonth()]+" "+d.getDate()+" , "+d.getFullYear()); } 
+2
source

Using dateFormat lib is available at the following link http://stevenlevithan.com/assets/misc/date.format.js

See this article for formatting js using the lib above. http://blog.stevenlevithan.com/archives/date-time-format

Edit: If you cannot use lib, then

var d = new Date ();

var curr_date = d.getDate ();

var curr_month = d.getMonth ();

var curr_year = d.getFullYear ();

var formattedDate = curr_date + "+ curr_month +", "+ curr_year;

+1
source

You can use getMonth (including some / case keys for text), the getDate and getFullYear methods to create your string.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/prototype#Methods

+1
source

EDITED

You can use this function in your code:

 function getFormattedDate(input){ var pattern=/(.*?)\/(.*?)\/(.*?)$/; var result = input.replace(pattern,function(match,p1,p2,p3){ var months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Dec']; return (months[(p1-1)]+" "+p2<10?"0"+p2:p2)+" "+p3; }); alert(result); } 

And for reference. you can call this function as follows:

 getFormattedDate("11/18/2013"); 

OR you can also use this code.

 var date = new Date(); dateFormat(date,"mediumDate"); 

You can also find other various formats here.

0
source

try it

 function getCurrentDate(){ var now=new Date(); var date=now.getDate(); var year=now.getFullYear(); var months=new Array('jan', 'feb', 'mar' ... 'dec'); var month=months[now.getMonth()] return month + ' ' + date + ', ' + year; } 
0
source

Source: https://habr.com/ru/post/1447740/


All Articles