How can I set the time value of the anytime jQuery plugin? - javascript

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();

Related

how to get timestamp value with timezone abbreviation

Is it possible to attach/include timezone abbreviation along with the timestamp value?. the below code returns the value something like this eg:2-16-2017 # 16:7:20
I would like to include timezone abbreviation into this value? any thoughts
var currentdate = new Date();
var datetime = (currentdate.getMonth()+1) + "-"
+ currentdate.getDate() + "-"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();alert(datetime)
Thanks,
Muthu
I don't know whether you can get timeZone abbreviations but definitely, you can get the offset.
var offset = new Date().getTimezoneOffset();
console.log(offset);
One more option is to use toTimeString() instead of manually concatenating the time part:
currentdate.toTimeString() //"01:19:10 GMT+0400 (SAMT)"

Display the date to the user in different Dateformat depending where he is from

I got a function which converts my timestamp into a date and looks like this:
delivery: function(created) {
var date = new Date(created * 1000);
var formattedDate = ('0' + date.getDate()).slice(-2) + '/' +
('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear() + ' ,' +
('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
return formattedDate;
}
now I want to display the date in MM/DD/YYYY for the american user which come to my site, so I would like to implement an if-clause, which firstly looks where the user comes from and then display the date in MM/DD/YYYY if he is from america, and if he is from europe in DD/MM/YYYY
Well in the SAPUI5 documentation I just saw this:
// The source value is given as timestamp. The used output pattern is "dd.MM.yyyy HH:mm": e.g. 22.12.2010 13:15
oType = new sap.ui.model.type.DateTime({source: {pattern: "timestamp"}, pattern: "dd.MMM.yyyy HH:mm"});
but I don't really understand how it works if I do oType.formatValue(created); its not working so maybe someone with more experience can explain me where I have to put my timestamp which is stored under "created"
What's wrong with simply using
delivery: function(created) {
var date = new Date(created * 1000);
return date.toLocaleDateString() + " " + date.toLocaleTimeString();
}
exactly?
EDIT: For clarity, I've supplied the whole function, not just the return statement

User current time

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).

Get last 36 hrs timestamp in javascript & compare it with php timestamp

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();

Parsing date string and retrieving day with Javascript

I've had a look through some of the suggestions in similar answers here but I can't find much that helps me.
Say I have a string that contains a date and a number: 2014-06-24 00:00:00
How would I parse it in a way that I can return this: 2014-06-24 00:00:00 Tuesday
Using date.parse as such:
new Date(Date.parse('2014-06-24 00:00:00'))
gives me the following result:
Tue Jun 24 2014 00:00:00 GMT+0100 (GMT Daylight Time)
Use methods getDay(),getDate() etc. to extract fields and format resulting string.
There are several JS sprintf implementations:
https://stackoverflow.com/a/3932473/2053898
https://github.com/alexei/sprintf.js
html
<div id="demo"></div>
js
//var a = new Date();
var a = new Date(Date.parse('2014-06-24 00:00:00'))
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
year = a.getFullYear()
month = a.getMonth()
date = a.getDate()
hour = a.getHours()
minutes = a.getMinutes()
seconds = a.getSeconds()
day = a.getDay()
alert(year + "-" + month+ "-" + date + " " + hour + ":" + minutes + ":" + seconds + " " + days[a.getDay()])
document.getElementById("demo").innerHTML = year + "-" + month+ "-" + date + " " + hour + ":" + minutes + ":" + seconds + " " + days[a.getDay()]
you can check the demo here
http://jsfiddle.net/victor_007/7ohh7mjv/
http://momentjs.com is a great, powerful library for easy date manipulation. Worth using if you're doing a lot of it. To get 2014-06-24 00:00:00 Tuesday you could do (after linking in the library, of course).
var m = moment("2014-06-24T00:00:00");
var output = m.format('YYYY-MM-DD hh:mm:ss dddd');
But, really, check out the docs, because this is just the tip of the iceberg: http://momentjs.com/docs/#/parsing/string-format/

Categories