开发者

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

开发者 https://www.devze.com 2023-03-19 00:36 出处:网络
This question already has answers here: Compare 2 dates in format DD/MM/YYYY with javascript/jquery 开发者_高级运维(3 answers)
This question already has answers here: Compare 2 dates in format DD/MM/YYYY with javascript/jquery 开发者_高级运维 (3 answers) Closed 9 years ago.

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

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

Thanks Amit


I think this will help you

http://docs.jquery.com/UI/Datepicker/formatDate


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


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

Qusetion 1 (extracting time)

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)

Java Script Section,

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,

Here the comparison is done using a 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...


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

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


Date time in "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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消