I am looking for a time output using jQuery, for example, would be great to know what time it is on the visitor's browser and the current day (friday,saturday,monday, etc...).
Is there any way to do it only with jQuery? I don't really like the way javascript handles time issues.
If you recommend any plugin, please tell me wich.
Thanks so much!
Souza.
EDIT:
I'm looking to avoid substring javascript outputs, or convert the results.
Wouldn't be great to use
$("#setime").yourtime("day");
and give me the day?
or
$("#setime").yourtime("hour", 24format);
and give you the hour in any format you need?
?
Try:
var currentTime = new Date();
It's not jQuery but it will do what you want.
You also have:
var month = currentTime.getMonth()
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
To play with.
Perhaps this is what you were looking for?
http://crossbreeze.github.com/jquery-sensible-datetime/
Download here: https://github.com/crossbreeze/jquery-sensible-datetime
If not, here is plain JS for you to reuse
<script type="text/javascript">
var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"," Saturday"];
var monthname=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
function formatDate(d) {
var text = "";
text += weekday[d.getDay()] + " ";
text += d.getDate() + " ";
text += monthname[d.getMonth()] + " ";
text += d.getFullYear();
var hh = d.getHours();
var mm = d.getMinutes();
if (hh<10) hh = "0"+hh;
if (mm<10) mm = "0"+mm;
return text +" "+hh+":"+mm;
}
$(document).ready(function () {
var d = new Date();
$("#dateDiv").text(formatDate(d));
});
</script>
Related
I would like to know if there's a way to convert a timestamp to yyyy-MM-dd'T'HH:mm:ssXXX date format?
I can convert it to ISO using toISOString but it add Z at the end of the string.
Thank you.
var d = new Date();
var datestring = d.getDate() + "-" +
(d.getMonth() + 1) + "-" +
d.getFullYear() + "-T " +
d.getHours() + ":" +
d.getMinutes();
If you really do not want to use an external library like moment.js (which i would strongly recommend), as you stated in your comment, you will have to implement a function for that yourself, as regular javascript does not provide a function for this (as far as i know).
You can create an object of javascripts built-in Date class from a unix timestamp by using
var unixTimestamp = 1566394163;
//multiply with 1000 as Date expects milliseconds
var date = new Date(unixTimestamp * 1000);
Then you could build the output string yourself, by using something along this
var dateString = "";
dateString += date.getUTCFullYear()+"-";
dateString += date.getUTCMonth()+"-";
dateString += ...
On the other hand, if the Z at the end of the string is the only thing that bothers you about the format provided by toISOString() as a workaround you could use its output and remove the last character of it
var dateString = date.toISOString();
var newDateString = dateString.substr(0, dateString.length -1);
Please try using below code to convert timestamp to date format.
var date = new Date(timestamp*1000);
var year = date.getFullYear();
var month = months_arr[date.getMonth()];
var day = date.getDate();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
Display date time in any format you want.
My time format is given below
var time = "2013-08-07 20:53:35"
I need to get the date "2013-08-07" as 07.08.2013 and time "20:53:35" as 20:53:35.
To achieve that you should
first separate date and time using split(" ")
Then split() date by "-"
Use reverse() on the date array and join it using .
var time = "2013-08-07 20:53:35"
let arr = time.split(' ');
let date = arr[0].split('-').reverse().join('-');
let tm = arr[1]
console.log(date)
console.log(tm)
depending on how much of this you will be doing I would highly recommend look at moment.js. You can download from npm if a nodejs app or just use from a cdn if browser based javascript. Here is a sample:
var time = "2013-08-07 20:53:35"
var newdate = moment(time).format("DD.MM.YYYY");
var newtime = moment(time).format("HH:mm:ss");
document.writeln(newdate);
document.writeln(newtime);
You can do like this using core javascript
function formatDate(datetime) {
var date = new Date(datetime);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var strTime = hours+':'+minutes+ ':' + seconds;
return ('0' + (date.getMonth()+1)).slice(-2) + "." +('0' + (date.getDate())).slice(-2) + "."+ date.getFullYear() + " " + strTime;
}
console.log(formatDate('2013-08-07 20:53:35'));
https://www.quackit.com/javascript/javascript_date_and_time_functions.cfm
http://jsfiddle.net/p2wLrov4/2/
I have a url that change every day based on today's date, for example:
http://www.newspaper.com/edition/20141227.html
where 20141227 is in the format YYYYMMDD.
Can I include the date using JavaScript? If possible, how would I do that?
I think following steps will help you to achieve the functionality your are looking for
1.Convert the today's date or any date to intended format that is "YYYYMMDD" in your case.
2.Then append it to your URL.
Please look into code snippet for details. Note you just need to hover over URL to know what it is pointing to.
Date.prototype.toMyString = function () {
//If month/day is single digit value add perfix as 0
function AddZero(obj) {
obj = obj + '';
if (obj.length == 1)
obj = "0" + obj
return obj;
}
var output = "";
output += this.getFullYear();
output += AddZero(this.getMonth()+1);
output += AddZero(this.getDate());
return output;
}
var d = new Date();
var link = document.getElementById("link");
link.setAttribute("href","/yourchoiceofURL?t="+d.toMyString());
<ul>
<li><a id="link" href="#">Any URL</a></li>
</ul>
var date = new Date().toDateString("yyyyMMdd");
then paste the date in building the URL
url = "http://blahblahblaj.com/"+date
You can try the below code. Hope this helps.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd};
if(mm<10){mm='0'+mm};
today = yyyy+mm+dd;
var new_url=document.URL+"/"+today+".html";
console.log(new_url);
Here's a simpler method that works
<script>
var link = document.getElementById('link'); // ref. to your anchor tag
var d = new Date,
date = d.getDate(),
month = d.getMonth()+1, // Months in JavaScript are 0 indexed
year = d.getFullYear();
if(date < 10) date = ("0" + date);
if(month < 10) month = ("0" + month);
link.href = ("STATIC_URL/" + year + month + date);//Concatenating three numbers, kind of a hack
</script>
This is as simple as it gets.
Thanks for all the answers colleagues. For Dnyanesh, I tried the code in http://jsfiddle.net/ can work well. I try to enter into an html page like this, why can not run perfectly. Where is the mistake?
<html>
<head>
<script type='text/javascript'>
Date.prototype.toMyString = function () {
//If month/day is single digit value add perfix as 0
function AddZero(obj) {
obj = obj + '';
if (obj.length == 1)
obj = "0" + obj
return obj;
}
var output = "";
output += this.getFullYear();
output += AddZero(this.getMonth()+1);
output += AddZero(this.getDate());
return output;
}
var d = new Date();
var link = document.getElementById("link");
link.setAttribute("href","http://www.pressdisplay.com/pressdisplay/pageview.aspx?issue=1245"+d.toMyString()+"00000000001001");
</script>
</head>
<body>
<a id="link" href="#">Any URL</a>
</body>
</html>
Thank you for all, all of the recommended code runs fine. If you want to put into the HTML code , add the following code to be loaded in the browser :
//<![CDATA[
window.onload=function(){
....javascript code here....
}//]]>
I want to make a link depend on current date.
Here is my Javascript:
<script type="text/javascript">
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
document.write(month + "" + day + "")
</script>
Above Javascript will show, example: 1124 (month/day)
I want to make this link follow the current day:
http://example.com/JS-RESULT
Today date is 24 11 2014 so the url will be like this
Today Budget
How can I make this work?
p.s: i love jquery.
JQuery Solution:
$(function(){
var currentTime = new Date();
var month = currentTime.getMonth() + 1
var day = currentTime.getDate();
$("#content").append("<a href='http://example.com/" + month + day + "'>Today Budget</a>");
});
JSFiddle: http://jsfiddle.net/ehLku1oq/
I am a little confused about your question, hopefully you are looking for one of the things below:
How to form the URL to example.com/1124?
Perform a simple string + string in javascript.
<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var date = month + "" + day + "";
var url = "http://example.com/" + date;
// Do what you want with the url
//-->
</script>
How "my manager just click the link and get into the current day" works?
Given that you have "one year list of links which have daily budgets", put all those webpages in the same folder as your current page which has Today Budget; or create a new folder called budgets for those daily budgets and change the url to http://example.com/budgets/1124.
My answer is very basic and I'm not sure if I got you correctly, let me know.
I want to set a text box with a date (in dd/mm/yyyy format) 14 days ahead to current date in javascript . can any one help me regarding this ?
This should do it:
var myDate=new Date();
myDate.setDate(myDate.getDate()+14);
then
document.getElementById(YOUR_TEXTBOX_ID).value = myDate.getDate() + "/" +
(myDate.getMonth() + 1) + "/" + myDate.getFullYear();
Date.js is a handy script for all kinds of JavaScript date manipulation. I've used it to make many date-based interfaces, including calendar controls.
Like Deodeus suggested, use Date.js:
var myDate = Date.today().add(14).days();
document.getElementById('mytextbox').value = myDate.toString('dd/MM/yyyy');
Following is the function to increment date by one day in javascript.
function IncrementDate(date) {
var tempDate = new Date(date);
tempDate.setDate(tempDate.getDate() + 1);
return tempDate;
}
Function calling...
var currentDate = new Date();
var IncrementedDate = IncrementDate(currentDate);