Convert Current date in IST format using Javascript - javascript

I want to convert current date value to the below format using javascript
2019-08-23T08:23:47+0530
I had tried the following code
var currentTime = new Date();
var currentOffset = currentTime.getTimezoneOffset();
var ISTOffset = 330; // IST offset UTC +5:30
var ISTTime = new Date(currentTime.getTime() + (ISTOffset + currentOffset)*60000);
and also try to convert to toUTCString(). But any of them gives the desired format.

try this it will work for ISO date
Date.prototype.toIsoString = function() {
var timezone = -this.getTimezoneOffset(),
DF = timezone >= 0 ? '+' : '-',
pad = function(nm) {
var narmal = Math.floor(Math.abs(nm));
return (narmal < 10 ? '0' : '') + narmal;
};
return this.getFullYear() +
'-' + pad(this.getMonth() + 1) +
'-' + pad(this.getDate()) +
'T' + pad(this.getHours()) +
':' + pad(this.getMinutes()) +
':' + pad(this.getSeconds()) +
DF + pad(timezone / 60) +
'' + pad(timezone % 60);
}
var date = new Date();
alert(date.toIsoString())

Related

Convert Round-Trip Time Pattern in ES6

I'm trying to convert something like this 2020-10-01T17:00:00.000Z to the correct time something like 06:00:00 PM. Pls see my code below:
const date = "2020-10-01T17:00:00.000Z";
const output = new Date(date).toLocaleTimeString('en-US');
console.log(output);
If you specifically need the time to be in UTC (instead of the local user's timezone), then I think you would need to build your own formatter function to do it, e.g.:
function formatDate(str, includeDate){
const date = new Date(str);
const hours = date.getUTCHours();
let twoDigits = (no) => {
const str = no.toString();
return (str.length === 1 ? '0' + str : str);
};
let strdate = (includeDate ? twoDigits(date.getUTCMonth() + 1) + '/' + twoDigits(date.getUTCDate()) + '/' + twoDigits(date.getUTCFullYear()) + ' ' : '');
if(12 <= hours) {
return strdate + twoDigits(hours === 12 ? 12 : hours - 12) + ':' + twoDigits(date.getUTCMinutes()) + ':' + twoDigits(date.getUTCSeconds()) + ' PM';
} else {
return strdate + twoDigits(hours) + ':' + twoDigits(date.getUTCMinutes()) + ':' + twoDigits(date.getUTCSeconds()) + ' AM';
};
};
console.log(formatDate("2020-10-01T17:00:00.000Z", true));
Here, you are constructing the time string from individual UTC segments. You could alternatively parse the input string using a regex, get the number of hours and then build the string that way.
Edit: updated to optionally also include the date, in US format mm/dd/yyyy.

Formatting date and time with zapier on 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.

Get timestamp in jquery in a particular format without a plugin

I want to get time stamp as follows in jquery 20140410091906. That is in the format YYYYMMDDHHMMSS.How can i do this without using any plugin.
You can do this:
new Date().toISOString().replace(/\D/g,"").substr(0,14);
toISOString() returns the date in year-month-dayThour:minutes:seconds.millisecondsZ format.
So I just removed the TZ-:. from the string and removed the milliseconds.
You can make your own format, for example:
var myDate = new Date();
So if you want to display it as mm/dd/yyyy, you would do this:
var displayDate = (myDate.getMonth()+1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
Try something like
function lpad(str, len, char) {
str += '';
if (str.length >= len) {
return str;
}
return new Array(len - str.length + 1).join(char) + str;
}
function getTs() {
var date = new Date();
var str = date.getFullYear() + lpad(date.getMonth(), 2, 0) + lpad(date.getDate(), 2, 0) + lpad(date.getHours(), 2, 0) + lpad(date.getMinutes(), 2, 0) + lpad(date.getSeconds(), 2, 0);
return str;
}
console.log(getTs())
Demo: Fiddle
This will do,
var d= new Date
d.toISOString().replace(/\D+/g,'').substr(0, 14)
Fiddle Demo
Date.prototype.YYYYMMDDHHMMSS = function () {
var yyyy = this.getFullYear().toString(),
mm = (this.getMonth() + 1).toString(),
dd = this.getDate().toString(),
hh = this.getHours().toString(),
min = this.getMinutes().toString(),
ss = this.getSeconds().toString();
return yyyy + (mm[1] ? mm : "0" + mm[0]) + (dd[1] ? dd : "0" + dd[0]) + (hh[1] ? hh : "0" + hh[0]) + (min[1] ? min : "0" + min[0]) + (ss[1] ? ss : "0" + ss[0]);
};
var d = new Date();
console.log(d.YYYYMMDDHHMMSS());

using date in javascript

I have trouble using date in Javascript, in PHP you use something like date("Y-m-d H:i s") to retrieve the specific date and time, how can I achieve this in Javascript? the getMonth() method only returns 1 digit, I really need them to be in 2 digits
Since I made comments on almost all answers, I'd better post my suggestion
DEMO
function pad(num) { return ("0"+num).slice(-2); }
function getDisplayDate() {
var date = new Date();
return date.getFullYear()+
"-"+pad(date.getMonth()+1)+
"-"+pad(date.getDate())+
" "+pad(date.getHours())+
":"+pad(date.getMinutes())+
":"+pad(date.getSeconds());
}
setInterval(function() {
document.getElementById("time").innerHTML=getDisplayDate();
},500);
why dont you add 0 before when you get <10
Try this :
function dateToYMD(date) {
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d) + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
}
var d = new Date();
alert(dateToYMD(d));
This code is adjusted based on the pointers given by #mplungjan -- (credits to him please)
Also check out his solution, which is a better one for use with date digits.
var str = 10; // month-digit from getMonth()
function pad(val) {
return "0" + val;
}
var month = str < 10 ? pad(str) : str;
console.log(month);
you can year, minutes, etc from Date class. You can get 2 digits month using some trick like example below. e.g
Date.prototype.getLongMonth = function() {
var month = this.getMonth();
if (String(month).length == 1) month = '0'.concat(month);
return month;
}
var now = new Date();
var theDate = now.getFullYear() + '-' + now.getLongMonth() + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
console.log(theDate); // result : 2013-02-17 12:41:2

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