I want to use a timestamp as an update indicator(last updated at), so i need a current time, month/day/year/hours/minutes/seconds, but the date() returns an live value. Is there a way to do this?
UPDATE: the idea is like this http://web.student.tuwien.ac.at/~e9125168/javas/jstamp.html (this shows a last modified time, but this is for the document).
The script where i need to show a 'last updated on' time is for an jquery ajax script, which updates a certain piece of code every ... seconds/minutes.
function getPastTimestamp(t) {
var d = new Date(t);
var output = "";
var items = new Array();
var i = 0;
items[i++] = d.getMonth() + 1;
items[i++] = d.getDate();
items[i++] = d.getFullYear();
items[i++] = d.getHours();
items[i++] = d.getMinutes();
items[i] = d.getSeconds();
for (i = 0; i < items.length; i += 1) {
output += (items[i] < 10) ? "0" + items[i] : items[i];
if (i < items.length - 1) output += '/';
}
return output;
}
function getCurrentTimestamp() {
return getPastTimestamp((new Date()).getTime());
}
Related
I'm working on my first project and I'm stuck with something. I created input values for my alarm, but I'm stuck with how to use the function to correlate it to the user’s selected values. I want to do something like,
let selectedHour = hour options from the info in the function.value or something and do that for mins and seconds of course.. I just don't know how. Thanks everyone.
This is the function I created to show alarm time selections:
function alarmMenu(){
let ahours = document.querySelector("#alarmhrs)
let hrs = 12
for (i=0; i <= hrs; i++) {
ahours.options[ahours.options.length] = new Option( i < 10 ? "0" + i : i, i);
}
let amins = document.querySelector("#alarmmins");
let min = 59;
for (i=0; i <= min; i++) {
amins.options[amins.options.length] = new Option(i < 10 ? "0" + i : i, i);
}
let asecs = document.querySelector("#alarmsecs");
let sec = 59;
for (i=0; i <= sec; i++) {
asecs.options[asecs.options.length] = new Option(i < 10 ? "0" + i : i, i);
}
}
alarmMenu();
If I understood you correctly, you want
let hrs = ahours.value;
let min = amins.value;
let sec = asecs.value;
This will set the value of each to the value of the input field.
I'd like to get a current timestamp object which is minutes l。 How do I do it with JavaScript?
Here my code :
var mins = "";
var new_timestamp = parseInt($("#current_time").data("timestamp")) + 1000;
var date = new Date(new_timestamp);
for (var b = 0; b < 60; b++) {
if (b == date.getMinutes()) {
str += "<option selected>" + (b < 10 ? ("0" + b) : b) + "</option>";
} else {
str += "<option>" + (b < 10 ? ("0" + b) : b) + "</option>";
}
}
$("#bank-order-time [name=\"minutes\"]").html(mins);
HTML :
<select name="minutes">
var date = new Date();
new Date() gives you a Date object of then time. You don't need to input a timestamp.
And date.getMinutes() give you the minute as you already know.
And if you need to get the current time again, remember you need to create a new Date object and do not use the old one.
My answer:
(function(){
var str ="";
var new_timestamp = parseInt($("#current_time").data("timestamp"))+1000;
var date = new Date(new_timestamp);
for( var a = 0; a < 24 ; a++)
{
if( a== date.getHours() )
{
str +="<option selected>"+(a<10?("0"+a):a)+"</option>" ;
}
else
{
str +="<option>"+(a<10?("0"+a):a)+"</option>" ;
}
}
$("#bank-order-time [name=\"hour\"]").html(str);
var mins = "";
for( var b = 0; b < 60; b++)
{
if( b == date.getMinutes())
{
mins +="<option selected>"+(b<10?("0"+b):b)+"</option>" ;
}
else
{
mins +="<option>"+(b<10?("0"+b):b)+"</option>" ;
}
}
$("#bank-order-time [name=\"minutes\"]").html(mins);
})();
I am displaying the following code for next 5 days
function setDateTime() {
var timesOffice = (officeTimes[officeID] + "").split(",");
//alert(officeTimes[officeID]+":12:"+timesOffice[0]);
var dt = new Date(correctDate);
var dateOptions = "";
var firstdateString = "";
var totalDays = 5;
for (i = 0; i < totalDays; i++) {
var sateString = dt.getFullYear() + " " + monthNames[dt.getMonth()] + " " + (dt.getDate());
//console.log("i:"+i+"s:"+sateString);
dateFlag = 0;
var j = 0;
for (j = 0; j < timesOffice.length; j++) {
if (checkValidDateTime(sateString, timesOffice[j])) {
dateFlag = 1;
break;
}
}
dt.setDate(dt.getDate() + 1);
if (dateFlag == 0) {
totalDays++;
continue;
}
if (firstdateString == "") firstdateString = sateString;
dateOptions = dateOptions + '<option value="' + sateString + '">' + sateString + '</option>';
}
$(".date").html(dateOptions);
}
I want to exclude Sundays from this list
You can tell what day of the week a Date instance represents using its getDay function:
if (dt.getDay() === 0) {
// It's Sunday
}
else {
// It isn't
}
I figure you can take it from there... :-)
You can use the getDay method to get the day of the week:
function setDateTime() {
var timesOffice = (officeTimes[officeID] + "").split(",");
//alert(officeTimes[officeID]+":12:"+timesOffice[0]);
var dt = new Date(correctDate);
var dateOptions = "";
var firstdateString = "";
var totalDays = 5;
int i=0;
while(i<totalDays) {
if(dt.getDay() != 0) // exclude Sundays
{
var sateString = dt.getFullYear() + " " + monthNames[dt.getMonth()] + " " + (dt.getDate());
//console.log("i:"+i+"s:"+sateString);
dateFlag = 0;
var j = 0;
for (j = 0; j < timesOffice.length; j++) {
if (checkValidDateTime(sateString, timesOffice[j])) {
dateFlag = 1;
break;
}
}
if (firstdateString == "") firstdateString = sateString;
dateOptions = dateOptions + '<option value="' + sateString + '">' + sateString + '</option>';
i++;
}
dt.setDate(dt.getDate() + 1);
}
$(".date").html(dateOptions);
}
As T.J Crowder said, you can use Date.getDay() to get the current weekday in the week. Giving some integer from 0 to 6 where 0 is Sunday and 6 is Saturday.
To show the next weekdays I supposed we don't want to print a bunch of numbers on the screen, so we can use a weekdays array to go from numbers to their corresponding text:
var weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
Now we can just use a for loop to scroll through these, starting from the todays day you get from using .getDay(). Note that if you go over we want to go back to 0, so I'll use a separate variable j in the loop for that:
var weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var date = new Date();
var day = date.getDay(); // Current day
var numDays = 5; // Number next days
for(var i = day; i <= day + numDays; i++ ) {
var j = i;
j = j % weekdays.length;
console.log(weekdays[j]);
}
To show it can overflow and move back to Sunday, Here is a fiddle that prints the next 20 days.
To exclude Sunday, simply check that if(j != 0) , then print:
for(var i = day; i <= day + numDays; i++ ) {
var j = i;
j = j % weekdays.length;
if( j != 0 )
console.log(weekdays[j]); // Only prints non-Sundays
}
Although since today is Sunday, suppose you want to include Today's Sunday, but not the next Sundays. Simply change the if statement to if( j != 0 || i == 0 ), which will make an exception for the first element. Here is an example of that.
I need to have an array of dates for whole days of the last week, including the current day, for e.g
['05/06', '04/06', '03/06', '02/06', '01/06', '31/05', '30/05']
(format dd/mm)
how can i do this?
I know there is the Date() object, but other than that I'm stumped.
logic along the lines of:
var dates = [];
var today = new Date();
for (var i = 0; i<7; i++){
var date = today - (i+1);
dates.push(date);
}
So you want an array containing todays date and a further 6 elements, with todays date-1, todays date-2 etc...?
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
var str = tempDate.getDate() + "/" + tempDate.getMonth();
dates.push(str);
}
console.log(dates);
Output: ["5/5", "4/5", "3/5", "2/5", "1/5", "31/4", "30/4"]
If you need numbers with leading 0's, try this:
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
var str = pad(tempDate.getDate()) + "/" + pad(tempDate.getMonth());
dates.push(str);
}
console.log(dates);
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}
Output: ["05/05", "04/05", "03/05", "02/05", "01/05", "31/04", "30/04"]
Check this working sample, where all days are printed out:
http://jsfiddle.net/danyu/Tu5R6/6/
This is the main logic:
for(var i=7;i>0;i--)
{
tempDate.setDate(tempDate.getDate()-1);
output+=tempDate+"<br/>";
}
Modify it to store those days into your array.
The problem that i am having here is that when i minus back to then end of the month, instead of going back to the 29 or 28 of last month the program starts to minus months instead of days. Bellow is my full code and below that is the output it produces in the google spread sheet.
function trying(){
var date = new Date();
var datechange = new Date();
var array = new Array(7);
for (var i = 0; i < 7; i++) {
array[i] = new Array(0);
}
for ( var i = 0; i < 7; i++){
days = i + 8
datechange.setDate(date.getDate() - days);
var tabName = Utilities.formatDate(datechange, 'MST', 'yyyy-MM-dd').toString();
array[i][0] = tabName;
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Want");
sheet.getRange("B2:B8").setValues(array);
}
This are the dates that are produced.
05/07/2012
04/07/2012
03/07/2012
02/07/2012
01/07/2012
30/06/2012
30/05/2012
You have to define datechange inside your loop, and not outside:
var date = new Date();
for ( var i = 0; i < 30; i++){
days = i + 8
var datechange = new Date();
datechange.setDate(date.getDate() - i);
console.log(datechange);
}
Date.getDate() returns the date (1-31) - so what you are doing is not correct.
Instead try this:
var ONE_DAY = 24 * 60 * 60 * 1000; //in milliseconds
for ( var i = 0; i < 7; i++){
days = i + 8
datechange.setDate(date.getTime() - (days * ONE_DAY));
var tabName = Utilities.formatDate(datechange, 'MST', 'yyyy-MM-dd').toString();
array[i][0] = tabName;
}
This is how JavaScript dates work. See here for full details.