How to get Timezone by passing abbreviation? - javascript

I'm using Momentjs and as of now i'm getting date and time by doing the following steps
var TimeZone = moment.tz("Asia/Kolkata").format('LLLL');
but i need to get time and date by passing abbreviation for instance
var TimeZone = moment.tz("IST").format('LLLL');
How can i achieve this?

I don't know Momentjs but I can provide u JS solution to do the same thing.
As i have understood from ur problem that ".tz" method only accepts full time zone name instead of short form. So u can use JS prototypal inheritance to achieve this outcome.
What u have to do is make another method of ur own with a dictionary object and extend "moment" object with ur features. Refer the code:
var tzMap = {
"IST" : "Asia/Kolkata",
"UST" : "America/LA" // this I only took for example i don't know it exist
or not
}
moment.__proto__.timezone = function(tzStr){
if(Object.hasOwnProperty(tzStr)){
tzStr = tzMap[tzStr];
}
return this.tz(tzStr);
}
// now instead of using
// moment.tz("Asia/Kolkata").format('LLLL');
// U can use
var TimeZone = moment.timezone("Asia/Kolkata").format('LLLL');
// or
var TimeZone = moment.timezone("IST").format('LLLL');

Related

Cannot get Date format using getDateTimeInstance function

I have a requirement that I need to adjust the date inside the worklist view, based on the date format set in frontend system (in GUI).
My code for formatter.js file:
DatePriority: function (sVar1, sVar2) {
var oDateFormat;
var oDateFormatFromGUI = sap.ui.getCore().getConfiguration().getFormatSettings().getDatePattern("short");
oDateFormat = sap.ui.core.format.DateFormat.getDateTimeInstance({
pattern: "EEE " + oDateFormatFromGUI,
UTC: true
});
return oDateFormat.format(new Date(sVar1));
}
However, oDateFormatFromGUI sometimes does return value, but sometimes it is undefined.
Is there a specific reason for this behavior? How can I make sure that oDateFormatFromGUI always has the data? Is it because I define it in formatter file, and not at the controller level?
Thank you.

get current time as PT08H10M00S

how can i convert current time in format as PT08H10M00S. I use an Odata service for communication and which expects time in format as PT08H10M00S which is 8:10:00 in time. Is there any inbuild js function to do the same.
If PT is dynamic then you can add code to derive at runtime how ever you want
var d = new Date();
var customDate= 'PT'+ d.getHours()+'H'+d.getMinutes()+'M'+d.getSeconds()+'S';
console.log(customDate);

Full Calendar Get current date

I've been learning Ruby over the last year and I'm very new to JS so I'll try to explain this as best I can.
I am using Adam Shaw's full calendar plugin. All I want to do is get the current month I am viewing (and use that to limit how far in the future or past a user can navigate, but that's not the problem).
I can get the current date, sort of. But, because of my lack of JS knowledge I'm not sure how to access the date.
Here is the relevant section of the config file,
viewRender: function(view){
var maxDate = "<%= finish.strftime('%Y/%m/%d') %>";
var currentDate = $('#calendar').fullCalendar('getDate');
console.log(currentDate);
if (view.start > maxDate){
header.disableButton('prev');
}
}
When I inspect the console log I see this being output as I click through the months.
So as you can see it is displaying the current date in view. My question is how do I access the _d bit of the Moment variable so I can use it?
My understanding would be that the Moment is class instance and the stuff in the dropdown is like its attributes, would this be a correct interpretation?
To get the current date of the calendar, do:
var tglCurrent = $('#YourCalendar').fullCalendar('getDate');
This will return the date as a moment object. You can then format it as a string in the usual date format like so:
var tgl=moment(tglCurrent).format('YYYY-MM-DD');
For the actual time, use the format: YYYY-MM-DD LTS
FullCalendar's getDate returns a moment object, so you need moment's toDate() method to get date out of it.
So, in you code try:
console.log(currentDate.toDate());
and that should return a date object.
var moment = $('#YourCalendar').fullCalendar('getDate');
var calDate = moment.format('DD.MM.YYYY HH:mm'); //Here you can format your Date

MomentJS how to get formatted, but not localised representation?

I'm using MomentJS v2.8.4, and I'm trying to get formatted date like "31/12/2015"
myDate.format('DD/MM/YYYY') works fine until I set some "less English :)" localisation, e.g. Arabic. Then I get something like this ١٠/٠١/٢٠١٥, which is nice for the user, not so nice for API.
From MomentJS source code
format : function (inputString) {
var output = formatMoment(this, inputString || moment.defaultFormat);
// here I get correct "31/12/2015" format
return this.localeData().postformat(output); // this will return localized version
},
formatMoment function is not publicly exported...
Can you please suggest correct solution for this?
You could save the current locale() setting in a variable (i.e save the user's setting) and then explicitly set the locale value so that you can get your date format correct for your API call, then set the locale value back to the saved value.
Something like:
var userLocaleSetting = moment.locale();
moment.locale('en');
var myFormattedDate = myDate.format('DD/MM/YYYY');
moment.locale( userLocaleSetting );
One solution may be to return an object with the api and user formatted date.
format : function (inputString) {
var api = formatMoment(this, inputString || moment.defaultFormat);
// here I get correct "31/12/2015" format
var user = this.localeData().postformat(api); // this will return localized version
return {api: api, user: user};
},

How to use preciseDiff from readable-range.js Plugin

I am using Dynamic CRM 2013 and need to calculate date difference between 2 dates. I added to the form moment.js and readable-range.js.
All functions in moment.js are working fine. When it comes to preciseDiff from readable-range.js and use:
var bDt = new moment("2/22/2009");
var eDt = new moment("2/29/2016");
var dtDiff = moment.preciseDiff(bDt, eDt);
I am getting the following error:
Object doesn't support property or method 'preciseDiff'
Please advise.
Don't use the new operator with moment.
Also, if you are passing values in that format, you should provide a format string, otherwise values like 1/2/2014 might be interpreted as Jan 2nd in some regions, and Feb 1st in others.
Other than that, there's nothing wrong with your code.
var bDt = moment("2/22/2009", "M/DD/YYYY");
var eDt = moment("2/29/2016", "M/DD/YYYY");
var dtDiff = moment.preciseDiff(bDt, eDt);
Working jsFiddle here

Categories

Resources