javascript Javascript获取并格式化当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13513144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:00:31  来源:igfitidea点击:

Javascript get and format current date

javascriptdateformat

提问by

Possible Duplicate:
Formatting a date in JavaScript

可能的重复:
在 JavaScript 中格式化日期

I need to show a current datein format like this (some examples):

我需要以这样的格式显示当前日期(一些示例):

sep 10, 2012
nov 5, 2012

and so on.

等等。

Using javascriptI get a current date object

使用javascript我获取当前日期对象

var date  = new Date();

what I need to do next?

我接下来需要做什么?

采纳答案by Srinivas B

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());
 }

回答by Sandeep Manne

Use dateFormat lib available in following link http://stevenlevithan.com/assets/misc/date.format.js

使用以下链接中提供的 dateFormat lib http://stevenlevithan.com/assets/misc/date.format.js

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

有关使用上述 lib 格式化 js 的信息,请参阅本文。 http://blog.stevenlevithan.com/archives/date-time-format

Edit: If you cant use lib then

编辑:如果你不能使用 lib 那么

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;

var d = 新日期();

var curr_date = d.getDate();

var curr_month = d.getMonth();

var curr_year = d.getFullYear();

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

回答by looper

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

您可以使用 getMonth(包括文本的一些开关/大小写)、getDate 和 getFullYear 方法来构建您的字符串。

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

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

回答by Bhavik Patel

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 ref. you can call this function directly like this:

而对于参考。你可以像这样直接调用这个函数:

getFormattedDate("11/18/2013");

OR you can use this code, too.

或者您也可以使用此代码。

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

you can also find further different formats here.

您还可以在此处找到更多不同的格式

回答by Ram G Athreya

Try this

试试这个

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;
}