Creating a version number based on last modified date - javascript

My function looks like this:
$(document).ready(function(){
var d = new Date(document.lastModified);
// document.getElementById("lastup").innerHTML = d;
$("#lastup").html(d);
// Create a version number based on the date
var cy = new Date();
var y = cy.getFullYear()-2015; // ex: 2018 - 2015 = 3 third year of program
var mo = d.getMonth()+1; // ex: 2 = Feb
var dy = d.getDate(); // ex: 12 = Date of month
var v = 'v'+y+'.'+mo+'.'+dy; // ex: v3 = third year of program
$("#version2").html(v); // ex: v3.2.12
});
The goal being to create a version number based on the last time the page code was changed. You can see the year part is based on years the program has been in place. But the month and day take there values from 'd' which is the value of "new Date(document.lastModified)" put it all together and you get a version number. But in this case it gives me a new version number for every day, it does not hold the last modified data, it always increments based on the date. Somewhere I'm not seeing the obvious here. Why is it doing this?

Related

How to find future UTC offset based on specific date and time zone name using Angular?

Is there an equivalent for figuring out UTC offset by supplying date, time, time zone name using Angular? This is very easy in C# - example below:
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
string dateInput = "2022-11-15";
var parsedDate = DateTime.Parse(dateInput);
TimeSpan offset = tzi.GetUtcOffset(parsedDate); // this returns -5
string dateInput2 = "2022-03-20";
var parsedDate2 = DateTime.Parse(dateInput2);
TimeSpan offset2 = tzi.GetUtcOffset(parsedDate2); // this returns -4
The JavaScript getTimezoneOffset() method is used to find the timezone offset
let d = new Date(Date.parse("2022-03-21T06:38:30+0000"));
console.log(d.getTimezoneOffset()) // 120 minutes offset for me
After many hours spent on this I decided to give moment.js as suggested by Joosep.P to try some library and also by others. The below seems to work as expected. What is bizarre is if you try to use timezoneName other than where you currently are and if you debug, you will still see your own time zone name for the date, however the offset will be calculated correctly. I assume that this works because of proper setting .tz call.
var timezoneName = 'America/New_York';
var dateInput = '2022-11-15';
var parsedDate = moment.tz(dateInput, timezoneName);
var offseta = parsedDate.utcOffset()/60; // this returns -5
var offsetb = moment.tz(dateInput, timezoneName).format('Z'); // returns '-05:00' as a string
var dateInput2 = '2022-03-20';
var parsedDate2 = moment.tz(dateInput2, timezoneName);
var offset2a = parsedDate2.utcOffset()/60; // this returns -4
var offset2b = moment.tz(dateInput2, timezoneName).format('Z'); // returns '-04:00' as a string

Convert string to date and alter that date

Good day, I am generating 3 dates from a string, I hope the output was:
billing date: 2020/01/11
cutoff start: 2019/11/11
cuttof end: 2019/12/10
but I get the following:
billing date: 2020/11/10
cutoff start: 2019/11/10
cuttof end: 2019/12/10
I would like to know how javascript works with variables or what is the problem since everything is altered
var month = "Jan-20"
var coverage_month_obj = moment(month, 'MMM-YY').toDate();
var billing_date = new Date(coverage_month_obj.setDate(coverage_month_obj.getDate() + 10))
var cutoff_end = new Date(billing_date.setMonth(billing_date.getMonth() - 1))
cutoff_end = new Date(billing_date.setDate(billing_date.getDate() - 1))
var cutoff_start = new Date(billing_date.setMonth(billing_date.getMonth() - 1))
I would like to know how javascript works with variables or what is the problem since everything is altered
Put simply, calling setXXX on a javascript date variable updates that variable in place. ie, it is what we would call "mutable". You might have assumed dates were immutable and did not change in place.
To answer on a better way to achieve your goal, I'd suggest using the other functionality of momentjs to calculate your 3 dates from the given input string.
var month = "Jan-20"
var coverage_month = moment(month, 'MMM-YY');
//Jan-20 I need to convert it into date format and that the day is 11 (2020/01/11) cutoff start, are two months less from that date (2020/11/11) and cutoff end is one month less from Jan-20, but ends on day 10 (2020/12/10)
var billing_date = coverage_month.clone().add(10, 'days');
var cutoff_start = billing_date.clone().subtract(2, 'months');
var cutoff_end = billing_date.clone().subtract(1,'months').subtract(1,'day')
console.log("billing_date",billing_date);
console.log('cutoff_start',cutoff_start);
console.log('cutoff_end',cutoff_end);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Add 1 day to date from spreadsheet via Google App Script / Javascript- Month Keeps Reseting to current month

I am trying to set up a Google App Script function that grabs a date (formatted dd/mm/yy) from the last column of a spread, and creates a new column with the date + one day.
I have seen previous solutions and tried to use the same, i.e.newDate.setDate(lastDate.getDate()+1) but have had issues getting the value formatted correctly in the script. This is a variation of my code that I'm using to loop through for a year's worth of values to see what I get:
for (var i=0;i<365;i++){
var lastRow = outputSheet.getLastRow();
var newDate = new Date();
var lastDate = outputSheet.getRange(lastRow,1).getValue();
var newDateRng = outputSheet.getRange(lastRow+1,1);
Logger.log(lastDate + 1, typeof lastDate, typeof (lastDate + 1));
newDate.setDate(lastDate.getDate());
Logger.log(newDate);
newDate.setDate((newDate.getDate() + 1));
Logger.log(newDate);
var newDateFormatted = Utilities.formatDate(newDate, ss.getSpreadsheetTimeZone(), "dd/MM/YY");
Logger.log(newDateFormatted);
newDateRng.setValue(newDateFormatted);
}
With a start date of "01/03/2020", I get the following behaviour:
01/03/2020
02/05/2020
03/05/2020
...
31/05/2020
01/06/2020
02/05/2020
03/05/2020
...
31/05/2020
01/06/2020
02/05/2020
...
etc. All the way through the year. Although the day increase, the month seems to reset after the first day of the month.
As a note, I am specifically looking to pick the date off of the spreadsheet rather than using new Date as today and new Date +1 as tomorrow.
Thanks
You need to use a different variable in the loop otherwise you will always return to the same month.
Also avoid using strings for the result, keep date objects and display it properly.
The code goes like this :
function otherTest(){
var lastDate = SpreadsheetApp.getActiveSheet().getActiveCell().getValue();
var date = new Date(lastDate); // create new date object
var result = [];
for (var i=0;i<365;i++){
date=new Date(date).setDate(new Date(date).getDate()+1)
Logger.log('date='+new Date(date))
result.push([new Date(date)]);
}
SpreadsheetApp.getActiveSheet().getRange(1,2,result.length,1).setValues(result).setNumberFormat('dd/MM/yyyy');
}

calculate age javascript in adobe acrobat

I am having some difficulties getting a field to populate in an interactive PDF form. I am using a javascript to calculate the current age of client from 2 date fields (DateToday and ClientDOB) already in the form and I need it to populate a "ClientAge" field. The DateToday field automatically populates when the form is opened. I would like for the ClientAge field to populate after the user selects the ClientDOB.
This is what I am trying to have it do. Should be simple I would think.
DateToday - ClientDOB = ClientAge
Here is my code:
var DateToday_ = Date2Num(DateToday.formattedValue, "MM/DD/YYYY")
var ClientDOB_ = Date2Num(ClientDOB.formattedValue, "MM/DD/YYYY")
var diff = DateToday_ - ClientDOB_
ClientAge.value = Floor(diff / 365.25)
I am not sure why the ClientAge field will not populate once the ClientDOB has been selected. Any replies would be helpful. Thanks.
This was taken from somewhere off the 'net. Can' remember where. However I have used this in a number of forms and it works fine. The idea is that the difference between dates is in milliseconds, and a given date is the number of seconds from a fixed date in the past. Once you have the difference in seconds between the dates (in this case DOB to the present) you can calculate how many years that is. Note that my format is in British date format (dd/mm/yy). If you operate in American format (mm/dd/yy) you must make the appropriate changes.
// get current date THIS NON AMERCAN DATE FORMAT
var oNow = new Date();
// get date from 'Demo.DOB' field
var oMyDate = util.scand('dd/mm/yy', this.getField('Demo.DOB').value);
// define second in milliseconds
var nSec = 1000;
// define minute in milliseconds
var nMin = 60 * nSec;
// define hour in milliseconds
var nHr = 60 * nMin;
// define day in milliseconds
var nDay = 24 * nHr;
// compute today as number of days from epoch date
var nNowDays = Number(oNow) / nDay;
// truncate to whole days
nNowDays = Math.floor(nNowDays);
// compute inputted date days from epoch data
var nMyDateDays = Number(oMyDate) / nDay;
// truncate to whole days
nMyDateDays = Math.floor(nMyDateDays);
// compute difference in the number of days
var nDiffDays = nNowDays - nMyDateDays;
// adjust difference for counting starting day as 1
++nDiffDays;
// convert days to years
var nYears = nDiffDays / 365.2525
// truncate to whole years
nYears = Math.floor(nYears);
// set field value number of years (nYears)
event.value = nYears;

How can I make Datejs return only the year when only the year is given?

I'm using Datjs to take a date from a user and convert it to a format suitable for storage in my database. When I supply a date with month, day, and year, I get just what I want, the date formatted year-month-day:
var d1 = Date.parse('02/22/1984');
console.log(d1.toString('yyyy-MM-dd')); //Prints '1984-02-22'
However, when I give a year only, I get back the same year, followed by today's month and day:
var d1 = Date.parse('1984');
console.log(d1.toString('yyyy-MM-dd')); //Prints '1984-12-19'
What can I do to ensure that when the user types in nothing but a year that just that year is returned in the following format
1984-00-00
Likewise, if only the month and year are give I'd like it formatted like this:
1984-02-00
Datejs returns a JavaScript Date object. This object is intended to represent a point in time and a point in time necessarily includes the month and day. When not provided, Datejs defaults these values to the current month and day. If you don't want to display that information, then change your formatting pattern:
var d1 = Date.parse('1984');
console.log(d1.toString('yyyy')); // prints 1984
If you need to change the pattern based on what the user originally entered, then you need to save that information, so that you know what to print later.
A simple example follows:
function DatePrinter(input) {
var s = input.split("/").length;
this.fmt = ["dd", "MM", "yyyy"].slice(3 - s).reverse().join("-");
this.date = Date.parse(input);
}
DatePrinter.prototype.toString = function() {
return (this.date.toString(this.fmt) + "-00-00").slice(0, 10);
}
Some tests:
new DatePrinter("02/22/1984").toString() // "1984-02-22"
new DatePrinter("02/1984").toString() // "1984-02-00"
new DatePrinter("1984").toString() // "1984-00-00"

Categories

Resources