Show yyyyww (yearweek) in Google Scripts / Javascript (Sunday-Monday) - javascript

I'm really struggling to do a clean, efficient way of showing yearweek for Sunday to Monday
e.g.:
201624
201625
201626
201627
201628
Is there an efficient way to do so in Google Scripts or Javascript without using a library?
Thanks!

Found a solution! :
var yearMonth = parseInt(Utilities.formatDate(new Date(), SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "yyyyww")).toFixed(0);
It was provided by user: Balu Ertl at Get week of year in JavaScript like in PHP

I would use Moment.js. Documentation for the use can be found here for the format you are looking for http://momentjs.com/docs/#/displaying/.
To have it work with google script you will need to first add it to the libraries using MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48, then you need to load it in using something like var moment = Moment.load().
From here you can get the month and year already formatted into your requested format.
var date = getDate();
var wwyy = moment(date).format('wo, YY');
My appologies, my format wasn't exactly as you wanted it to be. To answer in the format you are looking for:
var date = new Date();
var yyyyww = moment(date).format('YYYY ww');

Related

How to get the difference between two dates with php and symfony 4?

I am developing a holiday management project. There is an entity called Conge which includes date_departure, date_retrun and period. I made a form and the view. I would like to set the difference between the date of departure and the date of return. For example, if the user chooses dates from '04-05-2019' to '04-08-2019', how can I get and display the difference in days using javascript, php, and symfony4?
If you are always going to use the m-d-Y format for your dates you can use just PHP to give you the number of days difference.
$departure ="04-05-2019";
$arrival = "04-08-2019";
$departure = DateTime::createFromFormat('m-d-Y', $departure)->getTimestamp();
$arrival = DateTime::createFromFormat('m-d-Y', $arrival)->getTimestamp();
echo ($arrival - $departure) / (24*60*60); // 86400 might save some math
Use momentjs (https://momentjs.com/docs/#/displaying/difference/)
var departureRaw = $(".departure").val();
var departureDate = moment(departureRaw);
var returnRaw = $(".return").val();
var returnDate = moment(returnRaw);
var difference = departureDate.diff(returnDate, 'days');
for php, as mentionned here and here you can use ->diff() function. I don't think than Symfony have any function for that

Javascript in Google Sheets script: help using setNumberFormat

Hoping this is a simple problem for you lot. I have no coding knowledge at all. But been using the below script in a Google Sheet to grab changing data from another sheet and log it daily, appending as it goes. Can't remember where I found the script - if I did I'd go back and ask its creator. It's been working fine; only thing is I have to manually copy paste my preferred date format every day. So I'd like the script to print the date in "dd/MM/yyyy" format (while retaining hours and minutes data inside the cell). I've been reading and searching online, and experimenting for ages but can't figure it out. This is the base code:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("History");
var source = sheet.getRange("A2:C2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
};
I've tried placing setnumberformat in various places and nearly always get an error. Perhaps my best attempt, inspired by other examples I've seen, was to add these new lines:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("History");
var source = sheet.getRange("A2:C2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
var cell = SpreadsheetApp.getActiveSheet().getRange(2, 1, 979);
cell.setNumberFormat("dd/MM/yyyy");
};
I hoped this would format the entire date row (Row A) after appending the new data. Probably a clunky solution even if it worked, but it didn't. It's my only attempt so far that doesn't return an error! So, yay? But it doesn't change the date format. So I give up. Any ideas?
To specify the format for a specific cell
var cell = sheet.getRange("A2");
cell.setNumberFormat("dd/MM/yyyy");
To specify the format for a range of cells
var cells = sheet.getRange("A2:C2");
cells.setNumberFormat("dd/MM/yyyy");
Please see the documentation for Range and formats.
Just in case you need the time as well, follow this code.
var cell = sheet.getRange("A2");
cell.setNumberFormat("dd/MM/yyyy h:mm:ss AM/PM");

moment.js compare dates via .diff()

Im having problems when I try to compare 2 dates.
var end = moment(items[i].dateEnd).format('DD/MM/YYYY');
var now = moment().format('DD/MM/YYYY');
Example: now = '29/10/2015' and end = '30/06/2015'
Tried using .diff() function from moment.js without any result, like this:
end.diff(now);
Any help?
Thanks.
You can try with specifying input date format:
moment(items[i].dateEnd, 'DD/MM/YYYY').diff(moment());

Compare Current Date and Time to input of type DateTime

I have googled a bit and could not find an answer. So here is my situation.
I have an input of type dateTime. I want to compare the value picked (mobile app for blackberry) to the current date and time. if the selected date is in the future (bigger than date now) I have to show a simple error message. This is all done when the user tries to save the data.
I have tried code like this, but was unsucessfull.
var dateOfIncident = $('#AccidentDetailsDate').val();
var dateNow = Date.now();
if(dateNow > dateOfIncident)
{
// do my stuffs :)
}
This does not work... It passes that validation. I am very new to javascript myself. Any help would be greatly appreciated. I googled and could not find a solution that does not use anything fancy. I need to do it in javascript.
Thanks in advance.
Try this:
var dateOfIncident = new Date($('#AccidentDetailsDate').val()); // or Date.parse(...)
var dateNow = new Date(); // or Date.now()
if(dateNow > dateOfIncident)
{
// do your stuffs...
}
However, if this works may depend on what format your date-string is! You may want to consider this post as well.

Get time difference between two date strings

I want to develop a JavaScript function to calculate the activity of users based on the date in the server where the data is stored. The problem is that the date is a string like this:
2013-08-11T20:17:08.468Z
How can I compare two string like this to calculate minor and major time as in the example?
If you want to compare two dates just use this :
var dateA = '2013-08-11T20:17:08.468Z';
var parsedDateA = new Date(dateA).getTime();
var dateB = '2013-06-06T17:33:08.468Z';
var parsedDateB = new Date(dateB).getTime();
if(parsedDateA > parsedDateB) {
// do something
}
Assuming you need to do the comparisons client-side, the best way is to load the dates into Date objects using Date.parse. Then compare them using the functions provided for Date, such as getTime.
Try parse method:
var s = "2013-08-11T20:17:08.468Z";
var d = Date.parse(s);
As I have understood you in the right way, there is a good answer to your question here.
You can also look at this very good Library (DateJS).
If your problem was converting from the Date-String to js-Date look at this Page.

Categories