In Javascript how to check current datetime with three dates. i want to disable the radio button if date is pervious datetime.i need to validate the date according to current datetime
suppose a)10/2/2017 11:00 b)21/3/2018 11:20 c)28/4/2018 14:00
above three dates i need to block the radio button and stike the date in javascript
function formatAMPM(date) { // This is to display 12 hour format like
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
var myDate = new Date();
var displayDate = myDate.getMonth()+ '/' +myDate.getDate()+ '/' +myDate.getFullYear()+ ' ' +formatAMPM(myDate);
alert(displayDate);
var dt = Date.parse(displayDate);
var d = Date.parse(ts);
var d1 = Date.parse(ts1);
var d2 = Date.parse(ts2);
alert(d);
alert(dt);
There is a good library momemt you can use that to check whether date is current date. You can use isBefore
Also if you want to use JS only you can do the following:
Convert your date string in Date object and get the time in milliseconds and then compare it to Date.now().
Related
I've searched everywhere but I can't find how to do this. I'm using the Datetime-local input type using:
<input type="datetime-local" />
When a user enters the values, it is in this format on screen:
MM/DD/YYYY HH:MM AM/PM
However when the form is submitted, the datetime-local value appears in this format:
YYYY-MM-DD:THH:MM
I want to keep it in the same format as it's entered. I've searched but can't find a JavaScript that will grab the value for datetime-local and convert it to the MM/DD/YYYY HH:MM AM/PM format and set the time to either AM / PM. Current the time is in Military so anything above 12:59pm will show 13:00 for example. How can the military time also be converted?
You can use this function
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0'+minutes : minutes;
var year = date.getFullYear();
var month = date.getMonth();
month = month < 10 ? '0'+month : month;
var date = date.getDate();
date = date < 10 ? '0' + date : date;
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = month + '/' + date + '/' + year + ' ' + hours + ':' + minutes + ' ' + ampm;
return strTime;
}
console.log(formatDate(new Date));
If you want you can use moment.js in that you can format according to your need, but that library is bit heavy if you worried about your website size.
I m trying to implement a method which can understand which coutry you are living and show time time exactly for this country. for example if you are living in USA showing the time with am/pm but if you are living in the Turkey showing the time for 24 hour format. I found a way to conver 24 hour system to am/pm but after that i got clueless.. Any ideas how to achieve that ?
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
I think, you can use toLocaleTimeString method to show time exactly for that country:
var d = new Date();
d.toLocateTimeString('en-US'); // "12:43:04 PM"
d.toLocaleTimeString("ja-JP"); // "12:43:04"
Here's link to MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
I hope my answer will help you.
May the force be with you.
I am using the current format in order to product a JavaScript date:
var date = visitdate;
var newdate = date.split("/").reverse().join("-");
I would expect this to return 1900-01-01 for example, however what this actually returns is 1900-01-0100.
Iv'e tried using slice in order to trim this off but this just ends up slicing off the day instead and still adds the zeros. There seems to be no way of getting rid of them. Is there anyway to remove them?
You may write a function like below and whatever format needed you can change
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
hours = hours < 10 ? '0'+hours : hours;
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
var Month = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1;
var Date = date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
var strDate = Month + "/" + Date + "/" + date.getFullYear();
return strDate + " " + strTime;
}
Or in your case if you dont want time just remove strTime from return of function
Assuming the input is "01/01/1900 00:00" - add a .split(" ") in between like this:
"01/01/1900 00:00".split(" ")[0].split("/").reverse().join("-");
Or just skip the reverse() if your input is like you said "1900-01-01":
"1900/01/01 00:00".split(" ")[0].split("/").join("-");
You could also extract the information from your date string by using a regex like this:
var d = '1990/01/01 00:00';
var matches = d.match(/^(\d+)\/(\d+)\/(\d+) (\d+):(\d+)$/);
if(matches){
var year = matches[1]
, month = matches[2]
, day = matches[3]
, hour = matches[4]
, minutes = matches[5];
console.log(year+'-'+month+'-'+day);
}
I have the follow function that properly returns the date in the format required, but the value returned isn't respecting the local timezone. In this case, its 4 hours off. What would I need to modify in this function to make it add the proper offsets based on the users location?
Thanks!
function date_str(seconds) {
var dt = new Date(1970, 0, 1);
dt.setSeconds(seconds);
console.log(dt);
var month = dt.getMonth() + 1;
var date = dt.getDate();
var year = dt.getFullYear();
var hours = dt.getHours();
var minutes = dt.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
return month + '/' + date + '/' + year + ', ' + hours + ':' + minutes + ' ' + ampm;
}
Edit: Passing unix time to function 1396450616.505 which converts to Wed, 02 Apr 2014 14:56:56 GMT which returns Sent at 4/2/2014, 2:56 PM from the function itself. The time here is 10:56 AM EST.
Assuming that seconds is a unix epoch (UTC), you should just use
function date_str(seconds) {
var dt = new Date(seconds*1000);
console.log(dt);
…
instead. The get…() methods will respect the local timezone. If you don't want that, you should use the getUTC…() equivalents.
I would like to get this format:
2:18:00 pm
Using the sample code from w3Schools.com below, I can get the correct results from IE and FireFox. But when it comes to Chrome, I get the 24hr clock version where it is simply displayed this way:
14:18:00
In FF
new Date().toLocaleTimeString()
// 2:18:00 pm
function twoDigitPad(number) {
return ("0" + number).slice(-2);
}
function twelveHourTimeString() {
var date = new Date();
var hour = date.getHours();
var min = twoDigitPad(date.getMinutes());
var sec = twoDigitPad(date.getSeconds());
var ampm = hour < 12 ? "am" : "pm";
hour = hour % 12 || 12; // convert to 12-hour format
return hour + ":" + min + ":" + sec + " " + ampm;
}
date.getHours() returns an integer between 0 and 23, which hour % 12 || 12 converts to the 12-hour format.
date.getMinutes() and date.getSeconds() each return an integer, so you'll need to zero-pad those values when they're less than 10. Optionally, hour as well.
Use the following javascript code,
<script type="text/javascript">
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var curr_sec = d.getSeconds();
if (curr_hour < 12)
{
a_p = "AM";
}
else
{
a_p = "PM";
}
document.write(curr_hour + ":" + curr_min + ":"
+ curr_sec+ a_p);
</script>
The o/p would be as you expect,2:18:00 PM
Dealing with dates reliably cross-browser is a ball-ache in javascript- I would use the DateFormat library; http://blog.stevenlevithan.com/archives/date-time-format
then as he notes on the page, you can call it like so;
dateFormat(now, "h:MM:ss TT");
There are a few alternatives, but this one seems the most light-weight.