I have this code but its not working.How can i check if time is in AM or PM.
var time = $(id1).val();
var hrs = Number(time.match(/^(\d+)/)[1]);
var mnts = Number(time.match(/:(\d+)/)[1]);
var format = time.match(/\s(.*)$/);
//alert(format);
//alert(time);
if (format == "PM" && hrs < 12) {
hrs = hrs + 12;
}
if (format == "AM" && hrs == 12) hrs = hrs - 12;
You made a copy/paste mistake from my answer from three years ago (convert 12-hour hh:mm AM/PM to 24-hour hh:mm)
var format = time.match(/\s(.*)$/)[1];
Don't forget [1]
Related
Please this "15:00" is coming to me as string.
I want convert it into 3:00 pm; means I want to convert it from GMT to EST.
Please tell me how to do it by inbuilt function or by creating some own function?
I wrote a function which could fit your needs:
function convertTime(time){
var arr = time.split(':'); //splits the string
var hours = Number(arr[0]);
var minutes = Number(arr[1]);
var AMPM = "";
if(hours>=12 && hours != 24){ //checks if time is after 12 pm and isn't midnight
hours = hours-12;
AMPM = " pm";
}else if(hours<12){//checks if time is before 12 pm
AMPM = " am";
}
if(hours==24){//special case if it is midnight with 24
hours = hours - 12;
AMPM = " am"
}
if(hours==0){//special case if it is midnight with 0
hours = hours + 12;
}
//converts the Numbers back to a string
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10){//checks if the hours is 2 places long and adds a 0 if true
sHours = "0" + sHours;
}
if(minutes<10){//checks if the minutes is 2 places long and adds a 0 if true
sMinutes = "0" + sMinutes;
}
var result = sHours + ":" + sMinutes + AMPM; //sets string back together
return result;
}
https://jsfiddle.net/b059upu8/
I'm using momentJS to count number of days between a range of 2 dates. Then, I convert this number in months, weeks and days. I can't make it count weekends. I think I should use isoweek() but it's not working for. It seems to check today's day : var isoweek() = 2, because today is tuesday.
var startDate = $('#jrange .input1').val();
var endDate = $('#jrange .input2').val();
var a = moment([startDate], 'DD-MM-YYYY');
var b = moment([endDate], 'DD-MM-YYYY');
//difference in days
var diffDays = b.diff(a, 'days');
//convert diff in month weeks and days
//if my range is between 2 and 7 days
if (diffDays >= 2 && diffDays <= 7) {
$("#productRentLength").html("");
//does the range contains saturday and sunday
if (moment(diffDays).isoWeekday() == 6 && moment(diffDays).isoWeekday() == 7) {
diffDays -= 2;
var inDays = moment.duration(diffDays, "days").format("M [month] W [week(s)] D [days(s)] ");
$("#productRentLength").text(inDays);
}
} else {
$("#productRentLength").html("");
var inDays = moment.duration(diffDays, "days").format("M [month(s)] W [week(s)] D [days(s)]");
$("#productRentLength").text(inDays);
}
I have a javascript object of date and its value is:
Thu Dec 18 2014 07:29:44 GMT+0500 (PKT)
Now I want to get the time from above data object in following format:
7:29 A.M
How can I do that using javascript code.
Please Help!!
You can do a little string addition by doing the following:
var result = "";
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var amORpm = hours > 12 ? "P.M" : "A.M";
if(hours > 12) hours -= 12;
result = hours + ":" + minutes + " " + amORpm;
console.log(result); // Will get you your desired format
I am using the following code:
var myDate= new Date();
myDate.toLocaleString( );
var x= myDate.getDay();
var y=myDate.gethours();
var z=myDate.getMinutes();
The time is in 12 hour format.
Now I need to store meridiem (AM or PM) in a variable. How can I do that?
How about this:
var y=myDate.getHours();
var meridiem = (y<12)?"AM":"PM";
alert(meridiem)
getHours() returns the hour in a 24 hour format. A quick solution would be:
var meridiem = (y >= 12) ? "PM" : "AM";
Wrapped into a nice tiny function!
function getMeridiem()
{
var d = new Date();
return (d.getHours() < 12) ? 'AM':'PM';
}
Just need a bit of help with getting a div to fire between mon-fri 9-5:
HTML
<div class="liveperson">
<!-- Start liveperson code -->
<!-- End Liveperson code -->
</div>
jQ
$(document).ready(function() {
var rightNow = getHours();
var day = rightNow.getUTCDay();
if (day == 1-5 || rightNow == 9-13) {
$('.liveperson').show();
}
});
Can't tell what it is I'm doing wrong!
Any help would be muchly appreciated!
Thanks in advance people!
Try this:
var d = new Date();
var dayOfWeek = d.getDay();
var hour = d.getHours();
if (dayOfWeek > 0 && dayOfWeek < 6 && hour > 8 && hour < 17) {
$('.liveperson').hide();
}
dayOfWeek is 0-6, Sunday (0) through Saturday (6).
hour is is 0-23 (0 being 12 am, 23 being 11 pm).
jsFiddle example
// get current time
var d = new Date(),
hours = d.getHours(),
mins = d.getMinutes();
day = d.getDay();
// if day is mon-Fri and time is between 9am and 5:30pm
if(0 < dday < 6
&& hours >= 9
&& (hours < 17 || hours === 17 && mins <= 30)){
$('.liveperson').show();
};
This should do it.