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);
}
Related
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().
So we have multiple clients, that are in multiple time zones. I'm pulling some dates from an API, and the dates/times that are in this string are exactly what I need to display. I've been researching this, and digging for some time, and still haven't come up with a clear answer. The string coming in is formatted as such:
"2017-12-29T20:00:00"
What I'm wanting is to extract both the date and time as is, into two strings (no timezone offsetting, no matter where the viewer is located) but am having some issues doing so. Also hoping to format it in the correct fashion as well. Example:
"M/d/yyyy"
"hh:mm AM/PM" (12 hour)
I've tried numerous ways to battle this, and don't really want to just grab substrings, but am half tempted to do so. Any help is appreciated.
Consider just reformatting the string, it avoids all issues with the built-in parser and timezones:
function reformatTimestamp(s) {
function z(n){return (n<10?'0':'')+ +n}
var b = s.split(/\D/);
var h = b[3]%12 || 12;
var ap = b[3] < 12? 'AM':'PM';
return b[1] + '/' + b[2] + '/' + b[0] +
' ' + z(h) + ':' + z(b[4]) + ' ' + ap;
}
console.log(reformatTimestamp('2017-12-29T20:00:00')) // 12/29/2017 08:00 PM
I think it would be better to pad the month and day with a leading zero (but I'd also use an unambiguous date format like DD-MMM-YYYY rather than the peculiar m/d/y).
Use this code:
function formatAMPM(date) {
var hours = date.getUTCHours();
var minutes = date.getUTCMinutes();
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 str = "2017-12-29T20:00:00";
var dt = new Date(str + "Z");
console.log("M/d/yyyy");
console.log((dt.getUTCMonth() + 1) + '/' + dt.getUTCDate() + '/' + dt.getUTCFullYear());
console.log("hh:mm AM/PM");
console.log(formatAMPM(dt));
How to Covert This time to date time like " 08:45 PM "
json time code
{"time":1480797244,"short":false,"forceseconds":false}
i need covert this time (1480797244) need idea in jQuery or javascript
Use the below method to convert the timestamp to your required format. Check the Updated fiddle also
function formatAMPM(timestamp) {
date = new Date(timestamp * 1000)
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var year = date.getFullYear();
var day = date.getDate();
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 = day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ' ' + ampm;
return strTime;
}
var timeObj = {"time":1480797244,"short":false,"forceseconds":false};
alert(formatAMPM(timeObj.time))
I am trying to format a mysql datetime object in Javascript, but I only get NaN results.
The value from the database is for example this datetime object:
2015-08-27 21:36:03
In my JS I try to convert this object as follows:
var formattedDate = new Date(datetimeObj);
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes;
return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
How come that I when printing the variable, I get NaN/NaN/NaN 12:NaN?
Some browsers will not parse the string "2015-08-27 21:36:03" as a valid date. For best results, use a standard ISO date string, as in
2015-08-27T21:36:03Z
Try this:
<script>
var formattedDate = new Date("2015-08-27 21:36:03");
console.log(formatDate(formattedDate));
function formatDate(date)
{
var hours = date.getHours();
var minutes = date.getMinutes();
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes;
return date.getMonth() + 1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
</script>
Its the same code, just passed the input in your function..
Here you can find answer on your quetions, it something like this with RegEx
var dateString = "2010-08-09 01:02:03";
var reggie = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var dateArray = reggie.exec(dateString);
var dateObject = new Date(
(+dateArray[1]),
(+dateArray[2])-1, // Careful, month starts at 0!
(+dateArray[3]),
(+dateArray[4]),
(+dateArray[5]),
(+dateArray[6])
);
I need to create strings in javascript that contain the current time.
How can I create two strings like the following, but with the current timestamp?
itemTimestamp = "Itemized at 2014-05-01, 11:11 PM"
itemFilename = "itemized_at_2014_05_01_11_11_pm.png"
JSFIDDLE:
http://jsfiddle.net/gkQ7y/9/
function format(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
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; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime ="Itemized at "+year+"-"+month+"-"+day+", "+hours + ':' + minutes + ' ' + ampm;
return strTime;
}
var d = new Date();
document.getElementById("test1").innerHTML = format(d);
var replaced = format(d).replace(/[ :-]/g,"_").replace(",","");
document.getElementById("test2").innerHTML = replaced+".png";