I have a grid in which each row has a PHP format('Y-m-d H:i:s') date displayed.I wanted to apply a javascript filter which will show only those rows which has timestamp past 36hrs.
Whats the best way to first get past 36hrs timestamp & then compare that timestamp with displayed PHP timestamp using javascript.
Till now I tried using below code to get past 36 timestamp
var mydate=new Date();
mydate.setHours(mydate.getHours()-36);
dateInPhpFormat=mydate.getFullYear()+'-'+mydate.getMonth()+'- '+mydate.getDate()+"
"+mydate.getHours()+":"+mydate.getMinutes()+":"+mydate.getSeconds();
When I print dateInPhpFormat it shows wrong date.
Any help would be appreciated .
The only thing that could be wrong is that Javascript gives you the month, day, minues and seconds without leading zero. Also months are zero based, so you need to add 1 to the month.
For example month In JS: 3, in PHP(with 'm'): 03
You can add this leading zero yourself, like this;
var mydate=new Date();
mydate.setHours(mydate.getHours()-36);
var month = ('0' + (mydate.getMonth() + 1)).substr(-2);
var day = ('0' + mydate.getDate()).substr(-2);
var hour = ('0' + mydate.getHours()).substr(-2);
var minute = ('0' + mydate.getMinutes()).substr(-2);
var second = ('0' + mydate.getSeconds()).substr(-2);
dateInPhpFormat = mydate.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
This will give you a date string which is identical to php date('Y-m-d H:i:s')
JavaScript Date objects use zero based months for some reason. Try adding 1 to the month.
dateInPhpFormat = mydate.getFullYear() + '-' + (mydate.getMonth() + 1 ) +'-'+mydate.getDate() + " " + mydate.getHours() + ":" + mydate.getMinutes() + ":" + mydate.getSeconds();
Related
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));
I've a requirement where I have to show the Date timestamp in following format.
MM:DD:YYYY HH:MM:SS IST/CST/CDT.
I have used javascript date object to get the date and time. But I don't know how to get the timezone (IST or CST or CDT or etc) from the obj. Do we have any technique or native javascript Plugin (non-jQuery plugin) to get this timezone abbreviated value?
Check out moment.js library and its format method, with which you can format a date to include the time zone.
Below is pure JS code, without using any third party library
var x = new Date();
var tz = x.toTimeString().match(/\((.+)\)/)[1];
var month = x.getMonth() + 1;
month = month < 10 ? ('0' + month) : month.toString();
var date = x.getDate() < 10 ? ('0' + x.getDate()) : x.getDate().toString();
var hour = x.getHours() < 10 ? ('0' + x.getHours()) : x.getHours().toString();
var min = x.getMinutes() < 10 ? ('0' + x.getMinutes()) : x.getMinutes().toString();
var sec = x. getSeconds() < 10 ? ('0' + x.getSeconds()) : x.getSeconds().toString();
var output = month + ':' + date + ':' + x.getFullYear() + ' ' + hour + ':' + min + ':' + sec + ' ' + tz
I have a script that stores an action taken by a user. There's a column that contains datetime and originally I user NOW(), but that uses server time, which is a few hours off as compared to the user's actual time.
So I decided I'll use the time that I can get with JS. I've formatted it this way:
var now = new Date(),
isnow = now.getFullYear() + '-' + ('0' + (now.getMonth() + 1)).slice(-2) + '-' + ('0' + now.getDate()).slice(-2) + ' ' + ('0' + (now.getHours() + 1)).slice(-2) + ':' + ('0' + now.getMinutes()).slice(-2) + ':' + ('0' + now.getSeconds()).slice(-2);
I've tested and while the format works fine, the time is off by an hour. Is it because of the Daylight Savings Time? How do I get the actual local time for the user?
In your code wrote:
...('0' + (now.getHours() + 1)).slice(-2)...
Try to remove this plus one
Additional you can check if Day Savings Time with:
if (now.dst()) { alert ("Daylight savings time!"); }
Date.prototype.stdTimezoneOffset = function() {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.dst = function() {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
Based at answer similar issue
You should use the toISOString() method to convert the Date object to the ISO-8601 standard format:
now.toISOString();
The ISO-8601 date format puts the time information into a universal form which includes optional timezone information (likely the source of your issues).
The anytime jQuery plugin by default displays with the current date/time. The current date is perfect, but I want to set the time to midnight/00:00:00 for one "DateTimePicker" and the day's ultimate second (23:59:59) for the second one. How to do that?
UPDATE
Interesting - that causes the following to display in the anytime datetimepicker:
Mon, 29 Apr 2013 07:00:00
(actually, I can't see the last zero, but I'm assuming that's what it is).
On clicking on the component (is this the correct terminology for a jQuery plugin component/control instance?), the text reverts to:
2013-04-29 15:39:33 (current local time).
I reckon the 7 am jazz shown first has something to do with the UTC differential between here and...Greenwich, I would guess (I'm in California), but the fact that it reverts to the current local time (instead of remaining at 7 am, which I'm sure could be tweaked to accommodate the hour differential) makes this only entertaining rather than exhale-inducing.
UPDATE 2
I'm awarding a bounty to Marcacci ASAP, but stil, an interesting addendum to all this: Based on that code, I was able to work out what was needed if I just want to get up to the current second (when the page was loaded), rather than up to the second before midnight of the current day, which normally would be quite a few hours in the future:
var d = new Date();
var t = new Time();
var s = ('0' + (t.getHour() + 1)).slice(-2) + ":" +
('0' + (t.getMinute() + 1)).slice(-2) + ":" +
('0' + (t.getSecond() + 1)).slice(-2);
return d.getFullYear() + "-" + ('0' + (d.getMonth() + 1)).slice(-2) + "-" + ('0' + d.getDate()).slice(-2) + s;
However, oddly enough, the text/anytime DateTimePicker is blank when I use this code - until I click on it! Then it populates as I would expect it to...???
The other one (the "Begin Date") component, displays its value (midnight on the first of the month):
var d = new Date();
return d.getFullYear() + "-" + ('0' + (d.getMonth() + 1)).slice(-2) + "-" + "01 00:00:00";
...just fine.
You should just be able to manually set the input's value to today at midnight, and the plugin will pick up from there.
$('#dateInput').val(function(){
var d = new Date();
d.setHours(0,0,0,0);
return d.toUTCString();
});
EDIT
This should put it into the format that you're using:
$('#dateInput').val(function(){
var d = new Date();
return d.getFullYear() + "-" + (d.getMonth()+1) + "-" + d.getDate() + " 00:00:00";
});
EDIT 2
OK, so it's VERY stubborn about the format, but I got it working:
$('#field1').val(function(){
var d = new Date();
return d.getFullYear() + "-" + ('0' + (d.getMonth()+1)).slice(-2) + "-" + ('0' + d.getDate()).slice(-2) + " 00:00:00";
});
Check out the jsfiddle here.
Try the following code:
HTML
<input type="text" id="first" />
<input type="text" id="second" />
JavaScript
// Current date and time
var date = new Date();
// Set time to 00:00:00
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
$("#first").val(date.toUTCString());
// Set time to 23:59:59
date = new Date(date.getTime() + 23*60*60*1000 + 59*60*1000 + 59*1000);
$("#second").val(date.toUTCString());
// Apply plugin
$("#first, #second").AnyTime_picker();
how can I get the date in this format [mm/dd/yy] using javascript. I am struggling to get the 'year' to a 2 digit figure as opposed to the full 4 digits. Thanks!
var date = new Date();
var datestring = ("0" + (date.getMonth() + 1).toString()).substr(-2) + "/" + ("0" + date.getDate().toString()).substr(-2) + "/" + (date.getFullYear().toString()).substr(2);
This guarantees 2 digit dates and months.
Try this:
HTML
<div id="output"></div>
JS
(function () {
// Get current date
var date = new Date();
// Format day/month/year to two digits
var formattedDate = ('0' + date.getDate()).slice(-2);
var formattedMonth = ('0' + (date.getMonth() + 1)).slice(-2);
var formattedYear = date.getFullYear().toString().substr(2,2);
// Combine and format date string
var dateString = formattedMonth + '/' + formattedDate + '/' + formattedYear;
// Reference output DIV
var output = document.querySelector('#output');
// Output dateString
output.innerHTML = dateString;
})();
Fiddle: http://jsfiddle.net/kboucher/4mLe1Lrd/
How About this for the year
String(new Date().getFullYear()).substr(2)
And since you need your Month from 01 through 12 do this
var d = new Date("2013/8/3");
(d.getMonth() < 10 ? "0" : "") + (d.getMonth() + 1)
Do the same thing for days, Minutes and seconds
Working Demo