How to create a note that include the day automatically? - javascript

I will like to see the way to obtain automatically the day in a note, to have a follow up,
// Add the date when you create a note
function Notes()
{
var app= SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet(); // Returns the active shee
var activeRange= activeSheet.getActiveRange() // Returns the active Range
activeRange.setNote(Date()); // Set the date in the note
}
This code is working but I obtain this: Thu Oct 22 2020 08:28:09 GMT+0200 (Central European Summer Time)
and I will only like to obtain 22/10/2020 08:28:09 for example.

You just need to convert the date to the desired format dd/MM/yyyy HH:mm:ss. You can use Utilities.formatDate() to achieve that:
function Notes()
{
var app= SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet(); // Returns the active shee
var activeRange= activeSheet.getActiveRange() // Returns the active Range
var today = Utilities.formatDate(new Date(), app.getActive().getSpreadsheetTimeZone(), "dd/MM/yyyy HH:mm:ss");
activeRange.setNote(today); // Set the date in the note
}

There are many ways to print a Date in JS
var d = new Date()
console.log(d.toString())
console.log(d.toISOString())
console.log(d.toDateString())
console.log(d.toGMTString())
console.log(d.toUTCString())
console.log(d.toTimeString())
console.log(d.toLocaleString())
console.log(d.toLocaleDateString())
console.log(d.toLocaleTimeString())
You can also build your own format using this getter methods:
var d = new Date()
console.log(d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate())
Keep in mind that the date is a local object, and depends from the browser locale and timezone.

Related

How can I prompt an abbreviate date with SpreadsheetApp.promp() in Google Sheets Scripts

I have a prompt that also displays some values to the user, one of them being a date. The problem is: this displays a too detailed date (ex.: Tue Mar 30 2021 06:29:23 GMT-0400 (Hora de verĂ£o oriental norte-americana)). How can I abbreviate this date only for the prompt?
An alternative approach would be to use the Utilities.formatDate function:
function displayPrompt() {
const ss = SpreadsheetApp.getActive();
const formattedDate = Utilities.formatDate(new Date(),ss.getSpreadsheetTimeZone(),"EE MMM dd y");
const ui = SpreadsheetApp.getUi();
const response = ui.prompt('Title', formattedDate, ui.ButtonSet.YES_NO);
}
This would give you this prompt window:
If you need complete control of the output, use Utilities.formatDate, which returns a string:
var d = new Date();
var tz = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var formattedDate = Utilities.formatDate(d, tz, "yyyy-MM-dd");
Logger.log(formattedDate);
SpreadsheetApp.getActive().getSpreadsheetTimeZone() gives you the time zone for the parent spreadsheet, but you can alternatively hardcode a value from this list (e.g., "GMT").
You can use 2 methods, toLocaleDateString() or toDateString() to shorten your date in Apps Script, as seen below:
function myFunction() {
var d = new Date();
var date = d.toLocaleDateString();
var date2 = d.toDateString();
Logger.log(date);
Logger.log(date2);
}
These 2 methods will give you a shorten date format as seen here:

How to grab standard date from DateTime object in Javascript

My date is as followed 2020-05-13T18:00:00+01:00
I am trying to grab the time, but in standard format. In this case, it'll be 06:00:00 pm
May I have some guidance on how to achieve this?
I've tried the following.
vm.training.startDate = '2020-05-13T18:00:00+01:00`'
var start = new Date(vm.training.startDate)
var locale = start.toLocaleTimeString(); // Produced 1:00:00 PM (not the value I'm looking for)
var test = start.getTime(); // produced 1589389200000
var test1 = start.toTimeString(); // produced 13:00:00 GMT-0400 (Eastern Daylight Time)
var test3 = moment(vm.training.startDate, 'MM-DD-YYYY hh:mm A') // moment date object
This one is quite close but still slightly off
var test4 = moment(vm.training.endDate, 'HH:mm').format('hh:mm:ss a')// produced `08:30:00 pm`
Any tips would be greatly appreciated
Use momentjs format to convert a date string to the respective format by specifying the format string.
Docs: https://momentjs.com/docs/#/parsing/string-formats/
console.log(
moment("2020-05-13T18:00:00+01:00", "YYYY-MM-YYYY HH:mm").format("hh:mm:ss a")
);
<script src="https://momentjs.com/downloads/moment.js"></script>
You can pass the timezone option when calling toLocaleTimeString() method:
var options = { timeZone: 'Europe/London'};
var d = new Date('2020-05-13T18:00:00+01:00');
var n = d.toLocaleTimeString('en-UK', options);
console.log(n)

Why does moment.js not changing my timestamp into different timezone?

I have the following jquery script:
moment.tz.add('America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0');
var mytime = 1443548496;
var newyc= moment.unix(mytime).tz('America/New_York').toDate(); //mg
alert(mytime);
alert(newyc);
and it doesn't work - I see the alert that prints the given timestamp first 1443548496 and then the time in my timezone instead of timezone America/New_York
Tue Sep 29 2015 19:40:56 GMT+0200 (Central Europe Daylight Time)
Why it is not converted at this point and stored in a variable newyc?
Include this to your script
http://momentjs.com/downloads/moment-timezone.min.js
then
moment.tz.setDefault("America/New_York");
The moment.js toDate() function does not set the time zone on the returned object:
// from moment.js
function toDate () {
return this._offset ? new Date(+this) : this._d;
}
As a demonstration:
http://jsfiddle.net/b8o5cvdz/
$(function () {
var timestamp = 1443545881; // Was 2015-09-29 11:58:01 America/Chicago
var myMoment = moment.unix(timestamp).tz('Atlantic/Canary');
var myDate = myMoment.toDate();
alert('myMoment = ' + myMoment.toString());
alert('myDate = ' + myDate);
});
As a question, if you are using moment.js and moment-timezone.js why do you want it back into a Javascript Date object?

How To Convert String ' dd/mm/yy hh:MM:ss ' to Date in javascript?

I have Done this
var d='dd/mm/yy hh:MM:ss';
var d1=d.split(" ");
var date=d1[0].split("/");
var time=d1[1].split(":");
var dd=date[0];
var mm=date[1]-1;
var yy=date[2];
var hh=time[0];
var min=time[1];
var ss=time[2];
var fromdt= new Date("20"+yy,mm-1,dd,hh,min,ss);
Is there Any way to do it using JQuery OR JavaScript?
If you are looking for alternatives in jquery or Javascript , then you can go with Moment.js,where you can Parse, Validate, Manipulate, and Display dates in JavaScript.
example:
var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');
This will work regardless of timezone for the format dd/mm/yyy hh:mm:ss only. It also does not rely on third party packages:
let dtStr = "12/03/2010 09:55:35"
console.log(strToDate(dtStr)); // Fri Mar 12 2010 09:55:35
function strToDate(dtStr) {
if (!dtStr) return null
let dateParts = dtStr.split("/");
let timeParts = dateParts[2].split(" ")[1].split(":");
dateParts[2] = dateParts[2].split(" ")[0];
// month is 0-based, that's why we need dataParts[1] - 1
return dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0], timeParts[0], timeParts[1], timeParts[2]);
}
How about Date.parse()?
new Date( Date.parse("05/12/05 11:11:11") );
// Thu May 12 2005 11:11:11 GMT+0200 (CEST)
The output produced is in local timezone and will differ in browsers in different timezones.
We can convert any local date format to datetime datatype using moment js.
Syntax:
moment('<local date value>','<local date format>').format('<expected convert format>')
Example:
moment('26/05/1986 00:00', 'DD/MM/YYYY HH:mm').format("MM/DD/YYYY HH:mm");
then the output will be 05/26/1986 00:00
Cheers
Date#parse should be able to parse that if you split on a string. Id also recommend looking into the npm package moment for date manipulation
From your code it seems that you are trying to convert string into date and also you are trying to fetch previous month. If yes then you can reconstruct your code as below:
Date.prototype.SubtractMonth = function(numberOfMonths) {
var d = this;
d.setMonth(d.getMonth() - numberOfMonths);
return d;
}
$(document).ready(function() {
var dateString='2015-06-17T18:30:12';
var d = new Date(dateString);
alert(d.SubtractMonth(1));
});

Add Number of months to a Date in a text field

function addDays(){
var myDate =document.getElementById('treatdate');
var numberOfDaysToAdd = document.getElementById('resultdays');
var tset = numberOfDaysToAdd.value;
var result1 = myDate.value.addMonths(parseInt(tset));
var pldate= moment(result1).format('YYYY-MM-DD');
return pldate; }
'treatdate' is an id for a treatment date which is pulled from my database. Thanks in advance.
You can always use moment.js library for Date manipulations. http://momentjs.com/
For your problem you can do the following.
function addDays(){
var datefrmDb = $('#treatdate').val();
var monthstoadd= $('#resultdays').val();
var new_date = (moment(datefrmDb, "YYYY-MM-DD").add('months', monthstoadd)).format("YYYY-MM-DD");
return new_date;
}
If your date format is yyyy-MM-dd
var myDate = new Date(document.getElementById('treatdate').value);
this will a date and time.
Example:
var dd = new Date("2014-02-02 11:11:11")
console log
Sun Feb 02 2014 11:11:11 GMT+0000 (GMT Standard Time)
See if this snippet can help you understand how to manipulate dates. To change/add the months to a Date() object you can use the setMonth() method.
numberOfMonthsToAdd = 5; // the number of months that you want to add
date = new Date(); // creating a Date object
date.setMonth(numberOfMonthsToAdd); // adding the number of months
Note that you may add numbers beyond 12 - the object handles the date, and jumps to the next year.

Categories

Resources