Reputation: 1585
I am getting my current date time in below format
Fri Oct 04 2019 13:48:00 GMT+0530 (India Standard Time)
But i need this in below format
04-Oct-2019 19:18
I am getting first format
exports.date = function formatDate(vpDateObj) {
var vpDate = locale.parse(dateUtils.toString(vpDateObj), {
datePattern: "dd-MMM-yyyy HH:mm",
selector: "date"
});
alert(vpDate);
alert(convert(vpDate));
return vpDate ? vpDate.toString(vpDate) : " ";
};
and i am trying to convert into proper format like below
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
hours = ("0" + date.getHours()).slice(-2);
minutes = ("0" + date.getMinutes()).slice(-2);
return [date.getFullYear(), mnth, day, hours, minutes].join("-");
}
But here GMT+530 hours
is not getting added in time.
Please help
Upvotes: 0
Views: 1557
Reputation: 3529
Pretty sure what you need is here is to convert that locale date IST
into GMT
(which is equivalent to UTC
there is no quantifiable difference between the two) in the required format dd-MMM-yyyy HH:MM
function dateConverter(d) {
const monthList = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
/*Notice UTC methods are used to take care of 1st day of month and year*/
const yr = d.getUTCFullYear();
const mnt = monthList[d.getUTCMonth()];
const day = d.getUTCDate() < 9 ? "0" + d.getUTCDate() : d.getUTCDate();
const gmtHr = d.getUTCHours();
const gmtMin =
d.getUTCMinutes() < 9 ? "0" + d.getUTCMinutes() : d.getUTCMinutes();
return [day, mnt, yr].join("-") + " " + [gmtHr, gmtMin].join(":");
}
const result = dateConverter(new Date());
//alert("Formatted::" + result);
console.info("Formatted::", result);
Upvotes: 1
Reputation: 14702
Use locale.format, be aware that idian time is ahead of GMT by 5h30 not backward
should minus 5h30 hour :
require(["dojo/date/locale"
], function(locale) {
var vpDate = new Date("Fri Oct 04 2019 15:48:00 GMT+0530 (India Standard Time)");
console.log(vpDate);
var format3 = locale.format( vpDate , {selector:"date", datePattern:"dd-MMM-yyyy HH:mm " } );
console.log("dd-MMM-yyyy HH:mm -> ",format3);
});
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dijit/themes/claro/claro.css" rel="stylesheet" />
<script>
dojoConfig = {
parseOnLoad: true,
async: true
};
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dojo/dojo.js"></script>
Upvotes: 0