javascript 以“31/12/2010 03:55 AM”格式在javascript/jquery中获取日期时间

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

Get datetime in javascript/jquery with "31/12/2010 03:55 AM" format

javascriptjquery

提问by Amit

Please advice, how can i get date time in "31/12/2010 03:55 AM" format using either javascript or jquery

请建议,我如何使用 javascript 或 jquery 以“31/12/2010 03:55 AM”格式获取日期时间

Also i would like to compare 2 date times and need to find the greator of the 2 how can do that too?

另外,我想比较 2 个日期时间,并且需要找到 2 个日期时间中的较大者,我该怎么做呢?

Thanks Amit

谢谢阿米特

回答by jaychapani

回答by Sander van Knippenberg

You could always try something like this...

你总是可以尝试这样的事情......

var d = new Date("December 31, 2010 03:55:00");
alert (d);
var hourString;
var amPm = "AM";
if ( d.getHours() > 11 ) {
    amPm = "PM"
    hourString = "0" + (d.getHours() - 12);
} else {
    amPm = "AM"
    hourInt = "0" + d.getHours();
}


var formattedDate = "" + d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getFullYear() + " " + hourInt + ":" + d.getMinutes() + " " + amPm;
alert (formattedDate);

回答by Srikanth Venkatesh

You can get the time like this.

你可以得到这样的时间。

var dates=new Date("31/12/2010 03:55 AM");
var hour=dates.getHours());
var minutes=dates.getMinutes();
var seconds=dates.getSeconds();

Converts the time portion of a Date object to a string

将 Date 对象的时间部分转换为字符串

var timeString=dates.toTimeString());

Comparing two dates

比较两个日期

var date1=new Date("31/12/2010 03:55 AM");
var date2=new Date("31/1/2011 03:55 AM");

if(date1.getTime()>date2.getTime()){
    alert(" date1 is greater ");
}else{
    alert(" date1 is less");
}

回答by Harun

Use the following functions to extract time from Javascript datetime object,

使用以下函数从 Javascript datetime 对象中提取时间,

Qusetion 1(extracting time)

问题1(提取时间)

Var Date=new Date();

var HH=Date.getHours();//yeilds hours 

var mm=Date.getMinutes();//yields minutes

var ss=Date.getSeconds();//yields seconds

After this construct a string with the above results,

在此构造一个具有上述结果的字符串后,

  var Time=HH+':'+mm+':'+ss;

Question 2(comparing dates)

问题 2(比较日期)

Java Script Section,

Java脚本部分,

function CompareStartAndEndDate(sender,args) {
     var txtFromDate = document.getElementById('<%=txtFromDate.ClientID %>');
     var txtToDate = document.getElementById('<%=txtToDate.ClientID %>');

     var a = txtFromExpiryDate.value.split('/'); //split the date string received  using /(if it is in dd/MM/yyyy ). 
     var b = txtToExpiryDate.value.split('/'); //split the date string received using /(if it is in dd/MM/yyyy ). 

     var startDate = new Date(a[2], a[1] - 1, a[0]); //create a javaScript datetime object using the above date parts.
     var endDate = new Date(b[2], b[1] - 1, b[0]); //create a javaScript datetime object using the above date parts.  


     var dateStatus = IsDateGreater(endDate, startDate); //call a different function to find difference and get the result as boolean an our requirement. 

     if (dateStatus) {
         args.IsValid = false;
     }
     else {
          args.IsValid = true;
     }

}

 function IsDateGreater(DateValue1, DateValue2) {

      var date1 = DateValue1.getTime();
      var date2 = DateValue2.getTime();

     //date1-date2 yield date diff in milli seconds. 
      if (date1 < date2)
             return true;
      else
          return false;
   }

Aspx section,

Aspx部分,

Here the comparison is done using a asp.net CustomValidator,

这里的比较是使用 asp.net CustomValidator 完成的,

     <asp:TextBox ID="txtFromDate" runat="server" CssClass="txt-input"></asp:TextBox>
     <asp:TextBox ID="txtToDate" runat="server" CssClass="txt-input"></asp:TextBox>
     <asp:CustomValidator ID="valCustmCheckDate" runat="server" ErrorMessage="To date should be later than From date" ForeColor="Red" ValidationGroup="Group1" ClientValidationFunction="CompareStartAndEndDate"></asp:CustomValidator> //This validator will call the client side javascript function (CompareStartAndEndDate) first on the click of the button below since the validation group of the customvalidator and the triggering button is same (Group1).
     <asp:ImageButton ID="imgbtnAddLoginUser" runat="server" ImageUrl="~/Images/btn-add.gif" ValidationGroup="Group1" OnClick="imgbtnAddLoginUser_Click" />

Hope this helps...

希望这可以帮助...

回答by vikram bhatia

Date time in "31/12/2010 03:55 AM"

“31/12/2010 03:55 AM”中的日期时间



var dat = new Date(); 
dat.format("dd/m/yy h:MM tt"); 

Compare 2 date times and need to find the greator of the 2 how can do that too.

比较 2 个日期时间,并且需要找到 2 的大者,这也如何做到。



Date dt1 = new Date();
Date dt2 = new Date();

if (dt1.getTime() > dt2.getTime()) 
{     
  alert("The first date is after the second date!"); 
}