I can't get the time-stamp format in a spreadsheet cell to also include the time. Currently, it only produces the date. What do I need to change to get the time-stamp to show both date and time?
Like this:
3/17/2015 10:57:45
function onEdit(event)
{
var ss = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(r.getColumn() == 8){ //To check if update cell in Column, 1 means first column.
if(r.getValue() == "Resolved"){ //To check the value is equal to Resolved
ss.getRange('L'+r.getRow()).setValue(new Date()); //Set column B1 value to current date.
date = Utilities.formatDate(time, "GMT", "HH:mm:ss");
}else{
ss.getRange('L'+r.getRow()).setValue('');
}
}
You need to use:
var formattedDate = Utilities.formatDate(time, "GMT", "MM-dd-yyyy HH:mm:ss");
There is an example in the documentation:
Google Documentation - formatDate
In you code, get the time zone of the script like this:
var timeZone = Session.getScriptTimeZone();
date = Utilities.formatDate(new Date(), timeZone, "MM-dd-yyyy | HH:mm:ss");
Related
I'm using a google sheets script, which on the click of a button will add values to two fields.
The first will contain the date, the second the time.
For this, I use this piece of code:
var timestamp = new Date();
var date = Utilities.formatDate(timestamp, "GMT+1", "dd/MM/yyyy");
var time = timestamp.toLocaleTimeString('nl-BE');
Now, the issue is that the time is off by 6 hours.
The timestamp value does contain the correct time, the date variable gets the correct date, but the time seems to differ 6 hours after the 'toLocaleTimeString() function.
Use Utilities.formatDate() for time as well, like this:
const timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone(); // or 'GMT+1'
const timestamp = new Date();
const dateString = Utilities.formatDate(timestamp, timezone, 'dd/MM/yyyy');
const timeString = Utilities.formatDate(timestamp, timezone, 'HH:mm:ss');
console.log(`date and time in ${timezone}: ${dateString} ${timeString}`);
Learning examples in comments of E&P Plugin for WordPress
I found a few bugs myself, but still can't make the code work.
Case: Choose Arrival date (date-picker) and program duration from select field (5|7|14|30|60) in days. Generate Departure date
This code from plugin owner don't word at all
jQuery(document).ready(function(){
jQuery('[data-title="Departure"]').on('change',checkDates);
jQuery('[data-title="Program"]').on('change',checkDates);
});
function checkDates(){
var nbDays = jQuery('[data-title="Program"] option:selected').attr('data-price');
var startDate = moment(jQuery('[data-title="Arrival"]').datetimepicker('getDate'));
var endDate = startDate.subtract(nbDays,'days');
jQuery('[data-title="Departure"]').datetimepicker('setDate',endDate.toDate());
}
From first view I find that he use subtract instead of add
I using https://momentjs.com/docs/ and Format date and Subtract days using Moment.js
var startdate = moment();
startdate = startdate.subtract(1, "days");
is equal
var startdate = moment().subtract(1, "days");
So this is mine version:
jQuery(document).ready(function(){
jQuery('[data-title="Arrival"]').on('change',checkDates);
jQuery('[data-title="Program"]').on('change',checkDates);
});
function checkDates(){
var nbDays = jQuery('[data-title="Program"] option:selected').attr('data-price');
var startDate = moment(jQuery('[data-title="Arrival"]').datetimepicker('getDate'));
var endDate = moment().add(nbDays,'days');
jQuery('[data-title="Departure"]').datetimepicker('setDate',endDate.toDate());
}
Still not recalculate departure date on.click and count days not properly
I think you get a wrong end-date because you are doing moment().add(...) which gets the current date at the script execution (equelevant of Date.now()).
Try to use the startDate variable and using moment prototype add on that instead:
var endDate = startDate.add(nbDays,'days');
This will make moment add additional days to the startDate instead of the current date.
End result:
function checkDates(){
var nbDays = jQuery('[data-title="Program"] option:selected').attr('data-price');
var startDate = moment(jQuery('[data-title="Arrival"]').datetimepicker('getDate'));
var endDate = startDate.add(nbDays,'days');
jQuery('[data-title="Departure"]').datetimepicker('setDate',endDate.toDate());
}
I have a script that updates my database every day at 9:30am. I’m using Ember.js for my application and I need to write a function that checks if a timestamp is past 9:30am or not. The feature should always be showing data, so if it is before 9:30am it should show the previous day’s data and if it's after then it should show the current day's data. So essentially the function would return a correct timestamp depending on what time of day it is. I’ve tried this using moment.js but cannot figure it out. Any help would be great.
payload.forEach(function(value) {
// console.log("value: ", value );
var nineThirty = ' 09:30:00';
// Create date from input value
var inputDate = moment( value.updated_at ).format('MM/DD/YYYY');
var date = moment(inputDate + nineThirty);
console.log("input Date: ", date );
// yesterday
var yesterdayDate = moment().subtract(1, 'days');
var YD = moment( yesterdayDate ).format('MM/DD/YYYY');
var yesterday = moment(YD + nineThirty );
console.log("here: ", yesterday );
// Get today's date
var todaysDate = moment().format('MM/DD/YYYY');
var today_date = moment(todaysDate + nineThirty);
//... than something
});
use isBefore
this will check if now if 9:30 is before the current time
if true date = today's date else the date is yesterdays date
If(moment().set({hour: 9, minute: 30}).isBefore(moment())){
date = moment().format('MM/DD/YYYY');
}
else{
date= moment().subtract(1, 'days').format('MM/DD/YYYY');
}
I have date format returned as 05-Jan, 12-feb etc.. when i convert current date using date object in javascript . I did something like this
var curr = new Date(),
curr_year = curr.getFullYear(),
curr_month = curr.getMonth(),
curr_day = curr.getDay(),
today = new Date(curr_year, curr_month, curr_day, 0, 0, 0, 0);
console.log(today);
Here the today is returned as invalid date i needed the create a timestamp which should not include minutes secs and millisecs as zero for date comparison of month and date alone based on that i can categories .Is there way to dynamically create a date and compare those dates for given format.
And when i try to convert my date string using date object it returns year as 2001. how can i compare dates based upon current year.
For eg: in php i have used mktime to create a date dynamically from given date format and compare those results. Any suggestion would be helpful. Thanks.
You can leverage the native JS Date functionality to get human-readable date strings for time stamps.
var today = new Date();
console.log( today.toDateString() ); // Outputs "Mon Feb 04 2013"
Date comparison is also built in.
var yesterday = new Date();
yesterday.setDate( yesterday.getDate() - 1);
console.log( yesterday.toDateString() ); // Outputs "Sun Feb 03 2013"
console.log( yesterday < today ); //Outputs true
You can use the other built-in methods to fine-tune this comparison to be/not be sensitive to minutes/seconds, or to set all those to 0.
You said that you used mktime() in php, so what about this?
change to this :
var curr = new Date(),
curr_year = curr.getFullYear(),
curr_month = curr.getMonth()+1,
curr_day = curr.getDay(),
today = curr_month+'/'+curr_day+'/'+curr_year;
console.log(today);
(getMonth()+1 is because January is 0)
change the :
today = curr_month+'/'+curr_day+'/'+curr_year;
to whatever format you like.
I have found a way to convert the date into timestamp i have tried as #nbrooks implemented but .toDateString has built in date comparison which works for operator < and > but not for == operator to do that i have used Date.parse(); function to achieve it. Here it goes..
var curr = new Date(),
curr_year = curr.getFullYear(),
curr_month = curr.getMonth(),
curr_day = curr.getDate(),
today = new Date(curr_year, curr_month, curr_day, 0,0,0,0);
var dob = new Date('dob with month and date only'+curr_year);
if(Date.parse(dob) == Date.parse(today)){
//Birthdays....
}
This method can be used to create a timestamp for dynamically created date.Thanks for your suggestions.
I have a comboBox DateType with options Today, Yesterday, Week-to-date and Manual Entry.
I also have 2 textboxes - one for Start Date and one for End Date.
By default, the DateType will be set to Today's date.
For eg.: Let's say, current date is 01/11/2011 (Tues)
When DateType : Today
Start Date = 01/11/2011
End Date = 01/11/2011
When DateType : Week-to-date (Note: Each week will be `Sun to Sat`)
Start Date = 01/09/2012 (Sun)
End Date = 01/11/2012 (Current Date)
Few Variables:
var startDate = new Date(document.getElementsByName('Start Date').value);
var endDate = new Date(document.getElementsByName('End Date').value);
I am new to javascript, so could anyone help me frame a function that can modify the Start Date and End Date based on the DateType selected(as above).
I need the condition only for Week-to-date
The rest would be similar,I guess, so I can frame the remaining conditions.
Thanks in advance!
In response to the onchange event of the select control you can set a date range something like:
function setDateRange()
{
var periodSelection = dateseln.period.options[dateseln.period.selectedIndex].value;
var start;
switch (periodSelection)
{
case "Today":
start = new Date();
break;
case "WeekToDate":
start = new Date();
start.setDate(start.getDate() - start.getDay());
break;
}
dateseln.startDate.value = start;
dateseln.endDate.value = new Date();
}