none
Convert String Time to Time object and compare it with current time in j query/Java sacript RRS feed

  • Question

  • Convert String Time to Time object and compare it with current time in j query/Java sacript

    Example: I have var Time= 10:00 am 

    So i want to compare it with current time.

    if  var Time>=Current time then alert time is greater than current time.


    Shiv Sharma

    Thursday, November 28, 2019 4:59 AM

Answers

  • Hi Shiv,

    The following code for your reference.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
    $(function(){
    	var time = '10:00 AM';
    	var startTime = timeToDateObject(time);
    	var currentTime=new Date();
    	if(startTime>=currentTime){
    		alert("Time is greater than current time.");
    	}else{
    		alert("Time is less than current time.");
    	}
    });
    function timeToDateObject(stringTime){
    	var date = new Date();
    	var parts = stringTime.match(/(\d+):(\d+) (AM|PM)/);
    	if (parts) {
    		var hours = parseInt(parts[1]),
    			minutes = parseInt(parts[2]),
    			tt = parts[3];
    		if (tt === 'PM' && hours < 12) hours += 12;
    		date.setHours(hours, minutes, 0, 0);
    	}
    	return date;
    }
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, November 28, 2019 6:50 AM
    Moderator

All replies

  • Hi Shiv,

    The following code for your reference.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
    $(function(){
    	var time = '10:00 AM';
    	var startTime = timeToDateObject(time);
    	var currentTime=new Date();
    	if(startTime>=currentTime){
    		alert("Time is greater than current time.");
    	}else{
    		alert("Time is less than current time.");
    	}
    });
    function timeToDateObject(stringTime){
    	var date = new Date();
    	var parts = stringTime.match(/(\d+):(\d+) (AM|PM)/);
    	if (parts) {
    		var hours = parseInt(parts[1]),
    			minutes = parseInt(parts[2]),
    			tt = parts[3];
    		if (tt === 'PM' && hours < 12) hours += 12;
    		date.setHours(hours, minutes, 0, 0);
    	}
    	return date;
    }
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, November 28, 2019 6:50 AM
    Moderator
  • Hi Shiv,

    Any update?

    If my reply help you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best regards,
    Dennis

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, December 5, 2019 7:32 AM
    Moderator