Formatting date and time with zapier on javascript - javascript

I try to write this code to pass in a time and format it. It works on my IDE but when I pass it to zapier, it has an error.
This is my code
function dateConvert(dateobj,format){
var year = dateobj.getFullYear();
var month= ("0" + (dateobj.getMonth()+1)).slice(-2);
var date = ("0" + dateobj.getDate()).slice(-2);
var hours = ("0" + dateobj.getHours()).slice(-2);
var minutes = ("0" + dateobj.getMinutes()).slice(-2);
var seconds = ("0" + dateobj.getSeconds()).slice(-2);
var day = dateobj.getDay();
var months = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var dates = ["SUN","MON","TUE","WED","THU","FRI","SAT"];
var converted_date = "";
switch(format){
case "YYYY-MM-DD":
converted_date = year + "-" + month + "-" + date;
break;
case "YYYY-MMM-DD DDD":
converted_date = year + "-" + months[parseInt(month)-1] + "-" + date + " " + dates[parseInt(day)];
break;
}
return converted_date;
}
var date = input.VIP_2bParsed;
var format = "YYYY-MMM-DD DDD";
var converted_day = dateConvert(date,format);
output={converted_day: converted_day}
I have the following error: TypeError: dateobj.getFullYear is not a function
Full image of error here ERROR

Is VIP_2bParsed a variable you mapped in the Zap editor? If so, you'll want to access it with inputData.VIP_2bParsed instead of input.VIP_2bParsed.

Related

Converting string m/dd/yyyy HH:MM:SS to date dd-mm-yyyy in Javascript

I have a string that looks like '1/11/2018 12:00:00 AM' and I want to reformat it to dd-mm-yyyy.
Keep in mind that the month can be double digit sometimes.
You can use libraries like moment.js. Assuming either you do not want to use any external library or can not use it, then you can use following custom method:
function formatDate(dateStr) {
let date = new Date(dateStr);
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
return day + '-' + month + '-' + year;
}
console.log(formatDate('1/11/2018 12:00:00 AM'));
You can do somethink like this :
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
console.log(curr_date + "-" + curr_month + "-" + curr_year);
However best way is 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');
function convertDate(oldDate) {
var myDate = new Date(Date.parse(oldDate)); //String -> Timestamp -> Date object
var day = myDate.getDate(); //get day
var month = myDate.getMonth() + 1; //get month
var year = myDate.getFullYear(); //get Year (4 digits)
return pad(day,2) + "-" + pad(month, 2) + "-" + year; //pad is a function for adding leading zeros
}
function pad(num, size) { //function for adding leading zeros
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
convertDate("1/11/2018 12:00:00 AM"); //11-01-2018
Demo here

Add day(s) in a date in Javascript

I have textbox displaying date and have a button. the function in button is to add 7 days and display in textbox. my code:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}
The issue how to add a day to go to the next month.
Example the date in Textbox is 2014/08/25 when I click the button the date will be 2014/09/01
Just add 7 days to your date, date already handles change of month/year:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
addday.setDate(addday.getDate() + 7);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}
If you just do this
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
that means if date is 21.12.2014 the output will be 28.13.2014
function onNext() {
var startdate = document.getElementById('date').value;
var d2 = new Date(startdate);
d2.setMonth(d2.getMonth()+1);
d2.setDate(1); // you can set here whatever date you want
document.getElementById('date').value = d2.getFullYear() + '/' + d2.getMonth() + '/' + d2. getDate();
}
Use this function
function updateAb(s){//format dd/mm/yyyy chnage according to your need
var dmy = s.split("/");
var joindate = new Date(
parseInt(dmy[2], 10),
parseInt(dmy[1], 10) - 1,
parseInt(dmy[0], 10)
);
var data_days=7;
joindate.setDate(joindate.getDate() + data_days);
var cc=("0" + joindate.getDate()).slice(-2) + "/" +("0" + (joindate.getMonth() + 1)).slice(-2) + "/" +joindate.getFullYear();
document.getElementById("datepickerdisabled1").value=cc;
}

Combine date and time string into single date with javascript

I have a datepicker returning a date string, and a timepicker returning just a time string.
How should I combine those into a single javascript Date?
I thought I found a solution in Date.js. The examples shows an at( )-method, but I can't find it in the library...
You can configure your date picker to return format like YYYY-mm-dd (or any format that Date.parse supports) and you could build a string in timepicker like:
var dateStringFromDP = '2013-05-16';
$('#timepicker').timepicker().on('changeTime.timepicker', function(e) {
var timeString = e.time.hour + ':' + e.time.minute + ':00';
var dateObj = new Date(datestringFromDP + ' ' + timeString);
});
javascript Date object takes a string as the constructor param
Combine date and time to string like this:
1997-07-16T19:20:15
Then you can parse it like this:
Date.parse('1997-07-16T19:20:15');
You could also use moment.js or something similar.
For plain JavaScript:
combineDateAndTime = function(date, time) {
timeString = time.getHours() + ':' + time.getMinutes() + ':00';
var year = date.getFullYear();
var month = date.getMonth() + 1; // Jan is 0, dec is 11
var day = date.getDate();
var dateString = '' + year + '-' + month + '-' + day;
var combined = new Date(dateString + ' ' + timeString);
return combined;
};
You can concatenate the date and time, and then use moment to get the datetime
const date = '2018-12-24';
const time = '23:59:59';
const dateTime = moment(`${date} ${time}`, 'YYYY-MM-DD HH:mm:ss').format();
Boateng's example fails in cases where time consisted of hours, minutes, days or months that ranged from values 0-9 as getDate(), getMonth() etc... will return 1 digit in these cases and the time string will fail and an invalid date is returned:
function CombineDateAndTime(date, time) {
const mins = ("0"+ time.getMinutes()).slice(-2);
const hours = ("0"+ time.getHours()).slice(-2);
const timeString = hours + ":" + mins + ":00";
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const dateString = "" + year + "-" + month + "-" + day;
const datec = dateString + "T" + timeString;
return new Date(datec);
};
Unfortunately do not have enough rep to comment
David's example with slight modifications:
function CombineDateAndTime(date, time) {
var timeString = time.getHours() + ':' + time.getMinutes() + ':00';
var ampm = time.getHours() >= 12 ? 'PM' : 'AM';
var year = date.getFullYear();
var month = date.getMonth() + 1; // Jan is 0, dec is 11
var day = date.getDate();
var dateString = '' + year + '-' + month + '-' + day;
var datec = dateString + 'T' + timeString;
var combined = new Date(datec);
return combined;
};
Concate date and time with moment JS which also works on firefox,
let d1 = moment().format('MM/DD/YYYY');
let dateTimeOpen = moment(d1 + ' ' + model.openingTime).format('YYYY-MM-DD HH:mm:ss');
let dateTimeClose = moment(d1 + ' ' + model.closingTime).format('YYYY-MM-DD HH:mm:ss');
const date = "2022-12-27";
const time = "16:26:42";
new Date(`${date}T${time});
output
Date Tue Dec 27 2022 16:26:42 GMT+0100 (Central European Standard Time)

Ajax get Date in dd/mm/yyyy format

var d = new Date();
var today_date = d.getDate() + '/' + month_name[d.getMonth()] + '/' + d.getFullYear();
This is how I am getting a date. It works with a slight problem. For todays date 7th of June 2011 it returns 7/11/2011, what i want it to return is 07/11/2011?
Anyone know how?
Well, you could simply check the length of d.getDate()and if it's 1 then you add a zero at the beginning. But you would like to take a look at format() to format your dates?
Like so:
("0"+1).slice(-2); // returns 01
("0"+10).slice(-2); // returns 10
Complete example:
var d = new Date(2011,1,1); // 1-Feb-2011
var today_date =
("0" + d.getDate()).slice(-2) + "/" +
("0" + (d.getMonth() + 1)).slice(-2) + "/" +
d.getFullYear();
// 01/02/2011
Try this (http://blog.stevenlevithan.com/archives/date-time-format):
var d = new Date();
d.format("dd/mm/yyyy");
Try this, this is more understandable.:
var currentTime = new Date();
var day = currentTime.getDate();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
if (day < 10){
day = "0" + day;
}
if (month < 10){
month = "0" + month;
}
var today_date = day + "/" + month + "/" + year;
document.write(today_date.toString());
And result is :
07/05/2011

How to get correct gmt time in Javascript

For using the Amazon mechanical turk API I want to get the current GMT time and show it in ISO format
2011-02-24T20:38:34Z
I am wondering if there is any way to correctly get the gmt time and also be able to reformat it with ISO format. I can use something like now.toGMTString(); but it makes a string out of the date and it is hard to reformat it with ISO.
var year = now.getUTCFullYear()
var month = now.getUTCMonth()
var day= now.getUTCDay()
var hour= now.getUTCHours()
var mins= now.getUTCMinutes()
var secs= now.getUTCSeconds()
var dateString = year + "-" + month + "-" + day + "T" + hour + ":" + mins + ":" + secs + "Z"
You should be using UTC now instead of GMT. (Amounts to almost the same thing now, and it is the new standard anyway)
I believe this will work for you:
Number.prototype.pad = function(width,chr){
chr = chr || '0';
var result = this;
for (var a = 0; a < width; a++)
result = chr + result;
return result.slice(-width);
}
Date.prototype.toISOString = function(){
return this.getUTCFullYear().pad(4) + '-'
+ this.getUTCMonth().pad(2) + '-'
+ this.getUTCDay().pad(2) + 'T'
+ this.getUTCHours().pad(2) + ':'
+ this.getUTCMinutes().pad(2) + ':'
+ this.getUTCSeconds().pad(2) + 'Z';
}
Usage:
var d = new Date;
alert('ISO Format: '+d.toISOString());
Not much more different than every else's answer, but make it built-in to the date object for convenience
function pad(num) {
return ("0" + num).slice(-2);
}
function formatDate(d) {
return [d.getUTCFullYear(),
pad(d.getUTCMonth() + 1),
pad(d.getUTCDate())].join("-") + "T" +
[pad(d.getUTCHours()),
pad(d.getUTCMinutes()),
pad(d.getUTCSeconds())].join(":") + "Z";
}
formatDate(new Date());
Output:
"2011-02-24T21:01:55Z"
This script can take care of it
/* use a function for the exact format desired... */
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'}
var d = new Date();
document.write(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z

Categories

Resources