rid of date in edit line in excel js file - javascript

I have excel table with timings created with excel js.
I need to show timings only without dates (h:mm). I can do it in a cell, it's OK, but not in edit line, how can I fix it?(
time: new Date(moment.utc().hours(h).minutes(m).seconds(0))
{ key: 'time', width: 7, style: { numFmt: 'h:mm' } },
there is no this info in docs or google. I tried to create date by several ways, but it couldn't resolve my problem

Try this solution
// Create a workbook object
var workbook = new ExcelJS.Workbook();
// Add a worksheet
var worksheet = workbook.addWorksheet('Sheet1');
// Get the cell A1
var cell = worksheet.getCell('A1');
// Set the number format to 'h:mm'
cell.numFmt = 'h:mm';
// Get the current time in milliseconds
var currentTime = Date.now();
// Create a date object with the current time
var time = new Date(currentTime);
// Get the time components in UTC
var hours = time.getUTCHours();
var minutes = time.getUTCMinutes();
var seconds = time.getUTCSeconds();
// Create a date object with the UTC time components
var utcTime = new Date(0, 0, 0, hours, minutes, seconds);
// Set the cell value to the time with a newline character at the beginning
cell.value = '\n' + utcTime;

Related

How to convert string to ISOString?

I have 3 variables: (1)Date (2) StartTime (3) EndTime
I would like to bring them as two variables (1)Date and StartTime (2) Date and EndTime, so that I can create a google calendar event.
As per my understanding, in order to create a google calendar event I need to pass ISO String format for event timings. Can anyone check the below code and help me with the missing piece.
function createEvent(title,Dt,startTime,endTime,col) {
var calendarId = '_____#group.calendar.google.com';
Logger.log(Dt); //2016-07-21
Logger.log(startTime); // 11:55 AM
Logger.log(typeof(startTime)); //string
//Help Needed to convert + to ISO
var event = {
summary: title,
start: {
dateTime: startISO
},
end: {
dateTime: endISO
},
colorId: col
};
event = Calendar.Events.insert(event, calendarId);
Logger.log('Event ID: ' + event.getId());
You can use .toISOString() on the Date object to get an ISO String, but Google Calendar is requesting a slightly different format than this, but it is a quick fix. Start with a normal conversion:
(new Date()).toISOString(); // "2016-07-29T00:00:00.000Z"
var startTime = new Date();
var isoStartTime = startTime.toISOString();
If you need to make the Date from separate objects you can:
var yourDate = '2016-07-29';
var yourTime = '11:55 AM';
var startTime = new Date(yourDate);
startTime.setHours(yourTime.split(':')[0]); // 11
startTime.setMinutes(yourTime.split(':')[1].substr(0,2)); // 55
startTime = startTime.toISOString(); // "2016-07-29T11:55:00.000Z"
Then change it to what Google's looking for:
// To RFC 3339...
startTime.substr(0,startTime.length-5)+'Z'; // "2016-07-29T11:55:00Z"
Or
//if the "startTime = startTime.toISOString()" assignment happened
startTime.split('.')[0]+'Z';
//if startTime is a Date object, not a string
startTime.toISOString().split('.')[0]+'Z';
You can also (and probably preferrably) use numbers instead of strings for all that; if you pass hours and minutes separately it could look cleaner than that string operation:
var startTime = new Date(yourDate);
startTime.setHours(yourHours); // string or int
startTime.setMinutes(yourMinutes); // string or int

parse.com - how i can create query equal createdAt by date only with javascript

I want to find data by "createdAt" field but i need to search with date only (without time).
var d = new Date();
var query = new Parse.Query("TableName");
query.equalTo("createdAt", d);
What you basically have to do to generate two dates:
date at 0:0:0 time
date+1day at 0:0:0 time
Then search for:
query.greaterThanOrEqualTo('createdAt', date);
query.lessThan('createdAt', datePlusOne);
This effectively gives you the range of dateT0:0:0 - dateT23:59:59.99999 inclusive, but in a safe way
If you want to use pure JavaScript:
// assuming date is the date/time to start from
date.setHours(0, 0, 0, 0);
// hours/min/sec/ms cleared
var datePlusOne = new Date(date);
datePlusOne.setDate(datePlusOne.getDate() + 1);
You can also use the moment library to make your code easier to read/maintain. This library is also used server-side in parse.com, though it is an older version.
m1 = new moment(date);
m1.startOf('day');
m2 = new moment(m1);
m2.add(1, 'day');
// convert moment back to JavaScript dates
date = m1.toDate();
var datePlusOne = m2.toDate();
Full solution using moments:
var d = new Date();
var query = new Parse.Query("TableName");
var start = new moment(d);
start.startOf('day');
// from the start of the date (inclusive)
query.greaterThanOrEqualTo('createdAt', start.toDate());
var finish = new moment(start);
finish.add(1, 'day');
// till the start of tomorrow (non-inclusive)
query.lessThan('createdAt', finish.toDate());
query.find.then(function(results) {
// use results
});
If you are looking for results, filtered by "created today", you could do this:
var moment = require("moment");
var start = moment().sod() //Start of day
var end = moment().eod() //End of day
var query = new Parse.Query("myClass")
query.greaterThanOrEqualTo("createdAt", start.format());
query.lessThan("createdAt", end.format());
query.find({...});
Of course, if you are looking for a greater timespan than "today", you would go with Timothy's answer.
This code has been tested in Parse Cloud Code with Momentjs 1.7.2

Conversion of Date to UNIX Timestamp

I have a JavaScript function running in Google Drive folder of many spreadsheets that is currently logging dates into the log;
function convertDates(){
var ui = SpreadsheetApp.getUi();
SpreadsheetApp.getActiveSpreadsheet().toast('Conversion Started With First Row Date Cells','Conversion', 5);
Logger.log('Date Conversion Started');
var activeCell = SpreadsheetApp.getActiveSpreadsheet().getActiveCell();
var cell = activeCell.getValue();
Logger.log(cell);
}
How would one convert the output of 'cell' which currently looks like:
May 03, 2014 at 05:19PM
Into a Unix Time stamp within Javascript? I am inclined to believe that this can involve the Date() function but am unsure.
Thank's in Advance.
You can do this way to remove "at" and the sheet then recognizes the value as a valid date.
function convertDates(){
var sheet = SpreadsheetApp.getActiveSheet();
var range_input = sheet.getRange(1, 1);
range_input = range_input.getValue();
range_input = range_input.replace("at ", " ");
sheet.getRange(1, 1).setValue(range_input);
}
I am just replacing for one single cell. You can loop that to all the needed cell values.
Hope that helps!
If you want the get a string representation of the number of seconds since the UNIX epoch (january 1, 1970) you can use a code like this :
function convertDates(){
var ui = SpreadsheetApp.getUi();
SpreadsheetApp.getActiveSpreadsheet().toast('Conversion Started With First Row Date Cells','Conversion', 5);
Logger.log('Date Conversion Started');
var activeCell = SpreadsheetApp.getActiveSpreadsheet().getActiveCell();
var unixEpoch = new Date(1970,0,1,0,0,0).getTime();// difference between JS epoch and UNIX epoch in milliseconds
var cell = ((activeCell.getValue().getTime()-unixEpoch)/1000).toString();
Logger.log(cell);
activeCell.setValue(cell);// this will write it back to the sheet... be careful, it is not a date object anymore but a string... so the code won't work after that.
}
You have to remove "at" and to add a space after "05:19".
Look like this :
var date = new Date("May 03, 2014 05:19 PM");

concatenate date and time in google spreadsheet

I have a script that imports events from spreadsheets into calendar:
function caltest1() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 3; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
var cal = CalendarApp.getDefaultCalendar();
for (i in data) {
var row = data[i];
var title = row[0]; // First column
var desc = row[1]; // Second column
var tstart = row[2];
var tstop = row[3];
var loc = row[4];
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
}
}
The script works fine if my spreadsheet contains the data like:
Title Description Start Stop Channel
Mr Dear no drinks 5/6/2014 20:55:00 5/6/2014 21:57:00 what ever
But it does not work if I create my own date =CONCATENATE($D4, " ",$G4), given that D4 has a date and G4 has time combined into a single cell Date and time. I figured because it senses that concatenate creates a plain text and not a time formatting, but how can I fix it?
In spreadsheets, dates have a native value of an integer representing the number of days since december 31 1899 and time is a decimal value which is the fraction of a day ( 6 hours = 1/4 of a day for example , 0.25 day).
So when you add DATE+TIME (integer+decimal) in a spreadsheet you get a full date with time .
So the answer (as you noticed it in your comment on the other answer) is logically to ADD both values. That's actually the reason spreadsheets are build like that ! (to make it easy to use date and time)
Use the formula =D2+E2 in a new column and you get a complete date object directly useable in JavaScript.
In Javascript date and time are the same objects, there is no time object that has no date and no date without time : their native values are milliseconds counted from January 1 1970 at midnight (which is an integer).
Since the type of concatenated strings is text, you can convert them to date before calling the Calendar methods.
var tstart = new Date(row[2]);
var tstop = new Date(row[3]);
I successfully concatenated date and time by using
=text(A2,"dd/mm/yy") & ", " & text(B2,"HH:MM")
I concatenated date and time

Convert UTC time to specific zone

I have the following data:
var currentTime: 2013-07-11 15:55:36+00:00
var currentTimezone: Africa/Asmera
I need a way to convert the currentTime in UTC to a new time based on currentTimezone.
I've looked into Timezone.js and I'm having trouble implementing it (the directions on the site are a little ambiguous)
The code for the function I'm intending on using is included. Thanks :)
<script>
$("#storeTime").click(function(){
storeCurrentTime();
})
$("#getTime").click(function(){
retrieveTime();
})
$("#storeTimezone").click(function(){
var yourTimezone = $('#timezone-select').find(":selected").text();
tz = yourTimezone.toString();
storeCurrentTimezone(tz);
})
$("#convertTime").click(function(){
//get the most recent UTC time, clean it up
var currentTime = $('#RetrievedTime').html();
currentTime = currentTime.split(": ")[1];
$('#convertedTime').html("Converted Time: " + currentTime);
//get the saved timezone
var currentTimezone = $('#storedTimezone').html();
})
</script>
You're going to need to know the timezone offset, so some sort of dictionary with strings to numbers.
// assuming your dictionary says 3 hours is the difference just for example.
var timezoneDiff = 3;
Then you can just make a new time like this
// Assuming you have the proper Date string format in your date field.
var currentDate = new Date(currentTime);
// Then just simply make a new date.
var newDate = new Date(currentDate.getTime() + 60 * 1000 * timezoneDiff);
Update
I've written a javascript helper for this which you can find at:
http://heuuuuth.com/projects/OlsonTZConverter.js
I pulled the timezone data from the wikipedia page https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Usage is as follows once included the script.
var offset = OlsonTZConverter.GetUTCOffset("Africa/Asmera");
or if there is Daylight Savings in effect:
var offset = OlsonTZConverter.GetUTCOffset("Africa/Asmera",true);
These will throw if you pass an invalid timezone, but you can check if a timezone is valid with:
var isValid = OlsonTZConverter.Contains("Africa/Asmera");
or just look at the entire dictionary with:
var tzDict = OlsonTZConverter.ListAllTimezones();
Hope this maybe saves someone some time sometime :).

Categories