javascript Javascript中的日期函数格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21622422/
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-27 21:19:52  来源:igfitidea点击:

Date function format in Javascript

javascriptjquerysqldatedatetime

提问by Naruto

i'm passing start date as 7/02/2014in dd/mm/yyyyformat, to add one month extra for current passed date, first i need to convert the passed var item to Date format and then i can manipulate on it.

7/02/2014dd/mm/yyyy格式传递开始日期,为当前传递的日期添加一个月的额外时间,首先我需要将传递的 var 项目转换为日期格式,然后我可以对其进行操作。

if i try to get date like this, var newDate = new Date(startDate.val());then i will be getting new date as 7 july 2014, i.e format of date i'm getting in response is mm/dd/yyyy.

如果我尝试获得这样的日期, var newDate = new Date(startDate.val());那么我将获得 2014 年 7 月 7 日的新日期,即我收到的日期格式是mm/dd/yyyy.

i need output of date()to be in dd/mm/yyyyformat only. how to achieve this?

我需要的输出date()是在dd/mm/yyyy唯一格式。如何实现这一目标?

Is there any other function in jquery to do this?

jquery 中还有其他功能可以做到这一点吗?

回答by Vlad Nikitin

var dateArr = '7/02/2014'.split('/');
var date = new Date();
date.setYear(dateArr[2]);
date.setMonth(dateArr[1] -1); //month starts from 0
date.setDate(dateArr[0]);

回答by Prashobh

Use jquery

使用jQuery

var date = $.format.date(new Date(Date), 'dd/mm/yyyy'); 

https://github.com/phstc/jquery-dateFormat

https://github.com/phstc/jquery-dateFormat

回答by mehmetakifalp

Actually I recommend jQuery Datepicker. Because really simple and comprehensible.

其实我推荐jQuery Datepicker。因为真的简单易懂。

You dont have any edits because datepicker default options show like this : dd/mm/yyyy

您没有任何编辑,因为 datepicker 默认选项显示如下:dd/mm/yyyy

jQuery;

jQuery;

 $(function() {
   $( "#datepicker" ).datepicker();
 });

Html;

html;

 <p>Date: <input type="text" id="datepicker"></p>

Check it : http://jqueryui.com/datepicker/

检查它:http: //jqueryui.com/datepicker/

回答by THE ONLY ONE

You can try this one

你可以试试这个

var newDate = new Date("2/7/2014");
var dateformat = newDate.getDate();
dateformat+="/";
dateformat+=newDate.getMonth()+1;
dateformat+="/";
dateformat+=newDate.getYear();
alert(dateformat);