I have a text field that displays the date in mmmm d, yyyy (called 'Expiry Date') and I am trying to make three smaller fields that display just the day (d), the month (m), and the year (yyyy) in each field.
I have tried to import the data into each field using this code:
var sField = 'Expiry Date'
and then i would custom format it just to "d", "m", or "yyyy" as appropriate. In the little formatting preview window it would show the desired output, but the fields would still be blank.
What is also odd is that it will only work with formatting that starts with the month.
The field that Im getting the first date is created from another calculation if that makes it any different. 'Expiry Date' gets it's data from a field called 'date'. Here is the code in which it assigns an expiry date 30 days after the value of 'date'
// define the value for the date field
var sField = 'Date'
// define the format of the date string
var cFormatDate = 'mm/dd/yyyy';
// define some time constants
var fSecond = 1000; // number of milliseconds in one second
var fMinute = 60 * fSecond; // number of milliseconds in a minute
var fHour = 60 * fMinute; // number of milliseconds in an hour
var fDay = 24 * fHour; //number of milliseconds in a day
// get the field object's string value
var fTodayDate = this.getField(sField).value;
// convert the string value into a date object
var oDate = util.scand(cFormatDate, fTodayDate);
// convert the date object to a value in milliseconds
var fDate = oDate.getTime();
// add 30 days to value using the number of milliseconds in a day
var fNewDate = fDate + (30 * fDay);
// convert computed date value to date object
var oNewDate = new Date(fNewDate);
// set the field's value to the date string for the date object
event.value = util.printd(cFormatDate, oNewDate);
Thanks in advance!!
I don't know anything about Acrobat, but assume its Date objects conform to ECMA-262. By far the best method of converting a date string to a date object is to parse it yourself, do not leave it up to the Date function/constructor or Date.parse.
From your post, it seems the date string is like October 17, 2012. There's a function below to help with that.
The best way to add whole days is to add them to the date, so given a date object:
// Create a new Date object
var now = new Date();
// Copy it
var then = new Date(now);
// Add 30 days
then.setDate(then.getDate() + 30);
Note that adding 30 to dates after 28 January (or 29 January in a leap year) will end up in March.
Edit
Date string parse function:
// Expects mmm d, yyyy e.g. October 17, 2012 or Oct 17, 2012
function parseDateString(s) {
var months={jan:0, feb:1, mar:2, apr:3, may:4, jun:5,
jul:6, aug:7, sep:8, oct:9, nov:10, dec:11};
var d = s.split(/\s/);
return new Date(d[2], months[d[0].toLowerCase().substring(0,3)], parseInt(d[1],10));
}
I may be over simplifying things, but if you have a date field (let's name it "DATE1") that has the full date in it.
Couldn't you just copy that field 3 time, assigning "DATE1" to the name for all three. This would take the date that you typed into the original Date field and duplicate it throughout the other three. Then just go into the field properties, ensure all 4 boxes are assigned the format "Date" -- and then on your 3 smaller boxes, assign them a Custom Date Option of "dd", "mm", or "yy" respectively?
Related
I have a Node.js server that triggers function based on timezones. Specifically, I'm using moment-timezone and from a fixed date and time input I need to trigger action at that same input but in different time zones.
So if I set in my server that the action should be triggered at 1:00 pm UK time and the user is in New York, I want the action to be triggered at 1:00 pm in New York.
That's what I am doing now:
exports.time_to_trigger = function(hour, date) {
var user_timezone = "Asia/Tokyo";
// create date object from date + hour strings
var dateObj = moment(date + hour, process.env.DATE_FORMAT + " HH:mm a");
// create offset
var max_value = moment(dateObj).add(3, 'minutes');
var low_value = moment(dateObj).add(-3, 'minutes');
console.log(max_value); // -> moment("2018-01-25T13:03:00.000")
console.log(low_value); // -> moment("2018-01-25T12:57:00.000")
// get the now value of the user timezone
var user_now = moment.tz(moment(), user_timezone);
console.log(user_now); // -> moment.parseZone("2018-01-24T13:01:00.038+09:00")
console.log(user_now.isAfter(low_value)); // -> false
console.log(user_now.isBefore(max_value)); // -> true
return (
user_now.isAfter(low_value) &&
user_now.isBefore(max_value)
)
}
As you can see from the comment, this is not working as the comparison with isAfter and isBefore take into consideration the time zone that I converted on purpose not to have this problem. How can I solve this?
Your issue is that you use timezone to get user_now but not to create dateObj. So dateObj is missing the timezone offset and your 2 dates are not comparable as you would wish.
To have all your dates on the same timezone:
// create date object from date + hour strings
var dateObj = moment.tz(date + hour, process.env.DATE_FORMAT + " HH:mm a", user_timezone);
I have "startdate" field, that I'm getting its value. Plus I have three other fields: day,month,year. Which I'm also getting its values. Now I have to add day and month and year to startdate to get my new date. Can anybody help me with this, in order to maintain date conditions? e.g: so i don't add 12 days to 25 and get 37.
function calDate(){
var startDate;
var trs_jour = Ext.getCmp('JOUR').getValue();
var trs_mois = Ext.getCmp('MOIS').getValue();
var trs_annee = Ext.getCmp('ANNEE').getValue();
if(trs_jour!='' || trs_mois!='' || trs_annee!=''){
var d = new Date(Ext.getCmp('STARTDATE').getValue());
var year = d.getFullYear();
var month = d.getMonth()+1;
var day = d.getDate();
}
Ext.Date class defines some basic methods for handling dates.
Provides a convenient method for performing basic date arithmetic. This method does not modify the Date instance being called - it creates and returns a new Date instance containing the resulting date value.
Ext.Date.add ( date , interval , value )
PARAMETERS
date : Date
The date to modify
interval : String
A valid date interval enum value.
value : Number
The amount to add to the current date.
RETURNS : Date
The new Date instance.
Examples :
// Basic usage:
var dt = Ext.Date.add(new Date('10/29/2006'), Ext.Date.DAY, 5);
console.log(dt); // returns 'Fri Nov 03 2006 00:00:00'
// Negative values will be subtracted:
var dt2 = Ext.Date.add(new Date('10/1/2006'), Ext.Date.DAY, -5);
console.log(dt2); // returns 'Tue Sep 26 2006 00:00:00'
Date calculation issue in JavaScript on Browser. There are 3 parameters -
From Date, No. of days & To Date
From Date selected using calendar component in JavaScript = 30/10/2016
No. of days entered = 2
Based on no. of days entered "To Date" should be calculated, so as per above input of From date & No. of days calculated "To Date" value should be 01/11/2016 but due to some wrong calculation it's showing 31/10/2016.
Time Zone - Istanbul, Turkey
Please refer below image for code snipped -
As it is clear from code snipped that prototype JavaScript library being used.
dateUtil.prototype.addDays=function(date,noofDays)
{
var _dateData=date.split("/");
var _date=eval(_dateData[0]);
var _month=eval(_dateData[1]);
var _year=eval(_dateData[2]);
var newFormatedDate = new Date(""+_month+"/"+_date+"/"+_year);
var newAddedDate=newFormatedDate.getTime() + noofDays*24*60*60*1000;
var theDate = new Date(newAddedDate);
var dd = theDate.getDate();
var mm = theDate.getMonth()+1; // 0 based
if(mm<10)
mm="0"+mm;
var yy = theDate.getYear();
if (yy < 1000)
yy +=1900; // Y2K fix
var addedDate=""+dd+"/"+mm+"/"+yy;
return addedDate;
}
It seems noofDays*24*60*60*1000 logic is problem where DST is not being considered.
There are 2 timezone showing with the same code but with different date format.
Please could you advise any guidance or read-up on this.
Edit :
JavaScript code added.
Probably not worth posting the code since it has some fundamental errors that should not have survived the new millennium.
var _date = eval(_dateDate[0]);
Don't use eval. There are a small number of cases where it is appropriate, but in general, just don't use it. Ever. The above is the same as:
var _date = _dateDate[0];
Then there is:
var newFormatedDate = new Date('' + _month + '/' + _date + '/' + _year)
You started on the right track by avoiding parsing strings with the Date constructor by splitting the date string into it's parts. But then you undid that good work by creating a new string and parsing it with Date. Just use parts directly:
var newFormatedDate = new Date(_year, _month-1, _date)
which removes all the vagaries of Date parsing and is less to type as well. Also, Date objects don't have a format, so a name like date is fine.
To add n days, just add them to the date:
var date = new Date(_year, _month-1, _date)
date.setDate(date.getDate() + 2);
So your function can be:
function dateUtil(){}
/* Add days to a date
** #param {string} date - date string in dd/mm/yyyy format
** #param {number} noofDays - number of days to add
** #returns {Date}
*/
dateUtil.prototype.addDays = function(date, noofDays) {
var dateData = date.split('/');
var date = new Date(dateData[2], dateData[1] - 1, dateData[0]);
date.setDate(date.getDate() + +noofDays);
return date;
}
var d = new dateUtil();
console.log(d.addDays('23/09/2016',3).toLocaleString());
I've use +noofDays to ensure it's a number. Also, the SO console seems to always write dates as ISO 8601 strings in Z time zone so I've used toLocaleString to keep it in the host time zone.
Essentially I have two unix timestamps, representing the first and last days of a given month. Is it possible programmatically determine the timestamps for the first and last of the previous month?
For example, I have the following two timestamps:
1467331201 --> July 1, 2016
1469923201 --> July 31, 2016
Essentially, can I manipulate these two numbers in a consistent way in order to the unix time (or Date object) for June 1, 2016 and June 30, 2016, respectively? Problem that I'm running into is that you cannot simply subtract a given amount because the amount of days in a month is variable.
You could use this function:
function getPreviousMonthRange(unixTime) {
var dt = new Date(unixTime * 1000);
dt.setUTCDate(0); // flips to the last day of previous month
var unixLast = dt.getTime();
dt.setUTCDate(1); // back to the first day of that same month
var unixFirst = dt.getTime();
return [unixFirst / 1000, unixLast / 1000];
}
// given first and last date (only one is really needed)
var unixTimeFirst = 1467331201;
var unixTimeLast = 1469923201;
// get previous month's first & last date
var [first, last] = getPreviousMonthRange(unixTimeFirst);
// output
console.log('previous month first day: ', first, new Date(first*1000));
console.log('previous month last day: ', last, new Date(last*1000));
Take a look at the following example:
// Specify a timestamp
var timestamp = 1467331201;
// Create a date object for the time stamp, the object works with milliseconds so multiply by 1000
var date = new Date(timestamp * 1000);
// Set the date to the previous month, on the first day
date.setUTCMonth(date.getUTCMonth() - 1, 1);
// Explicitly set the time to 00:00:00
date.setUTCHours(0, 0, 0);
// Get the timestamp for the first day
var beginTimestamp = date.getTime() / 1000;
// Increase the month by one, and set the date to the last day of the previous month
date.setUTCMonth(date.getUTCMonth() + 1, 0);
// Explicitly set the time to 23:59:59
date.setUTCHours(23, 59, 59);
// Get the timestamp for the last day
var endTimestamp = date.getTime() / 1000;
// Print the results
console.log('Timestamps for previous month: ');
console.log('Begin timestamp: ' + beginTimestamp);
console.log('End timestamp: ' + endTimestamp);
A timestamp must be specified in the variable on the top, this might be one of the two timestamps you suggested in your question, anywhere in a month.
This code then calculates the begin and end timestamp for the previous month as you've requested, and prints the results to the console.
Please note, that in this example the begin timestamp uses 00:00:00 as time, and the end timestamp uses 23:59:59 as time (the last second of that day). This can be configured the way you'd prefer.
In this case, we're working with the ...UTC... Date functions, because a Unix timestamp is in UTC time, not in the timezone the user is in.
The statement date.setMonth(date.getMonth() + 1, 0); is used to select the last day in the month. The next month is selected first, but because the day is set to 0 (and not 1) one day is subtracted giving you the preferred result. This is described here.
You can consider using Moment.js. I'm sure this is not exactly how you'd end up handling it but see below for an example of some helpful methods.
var lastDayOfJuly = moment(1469923201);
var firstDayOfJuly = lastDayOfJuly.startOf('month');
var lastDayOfJune = firstDayOfJuly.subtract(1, 'day');
var firstDayOfJune = lastDayOfJune.startOf('month");
Moment.js
I am reading excel data using php and JavaScript. Storing results in variable and showing it on the page.
Simple code example:
var yearend = "< ? php echo ($connection->sheets[0]["cells"][2][5]); ? >";
This works for text and fields with number. But when I format cell as "Date" it returns the values, such as.
Excel field is: 31-Dec-2015 - JavaScript returns value: 40542
I know it is a MS DATEVALUE formatting.
But i need to convert it to date using JavaScript so it shows 31-Dec-2015 or 31 December 2015 only.
So in short:
From Excel 40542 to JavaScript 31 December 2015.
Also, I only need as above, without trailing time and locations, so removing:
00:00:00 00:00 GMT
Also is it possible modify the date to +1 day or -1 day?
//Convert Excel dates into JS date objects
//#param excelDate {Number}
//#return {Date}
function getJsDateFromExcel(excelDate) {
// JavaScript dates can be constructed by passing milliseconds
// since the Unix epoch (January 1, 1970) example: new Date(12312512312);
// 1. Subtract number of days between Jan 1, 1900 and Jan 1, 1970, plus 1 (Google "excel leap year bug")
// 2. Convert to milliseconds.
return new Date((excelDate - (25567 + 1))*86400*1000);
}
try this
toDate(serialDate, time = false) {
let locale = navigator.language;
let offset = new Date(0).getTimezoneOffset();
let date = new Date(0, 0, serialDate, 0, -offset, 0);
if (time) {
return serialDate.toLocaleTimeString(locale)
}
return serialDate.toLocaleDateString(locale)
}
Use the following php function to covert the datevalue into a php timestamp. You could then use standard date functions to format however you wish
function xl2timestamp($xl_date){
return ($xl_date - 25569) * 86400;
}