get current time as PT08H10M00S - javascript

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);

Related

How can I convert the value I get with datetimepicker to date and time?

var innerDate = document.getElementById('datetimepicker3').value;
The format I get from this line is: innerDate: "2021/07/07 14:00"
But I need to parse the date and time information here. How can I convert Start Date and Start Time into two separate variables?
I would recommend to parse that string with moments.js and then depending on your needs either get the individual parts for calculations or apply needed formatting
Example available at https://jsfiddle.net/Ly6qen3w/
let innerDateInput = "2021/07/07 14:00";
let innerDateMoment = moment(innerDateInput, "YYYY/MM/DD hh:mm");
console.log("Date:"+innerDateMoment.format("YYYY-MM-DD"));
console.log("Time:"+innerDateMoment.format("hh:mm"));
Try this:
var innerDate = new Date(document.getElementById('datetimepicker3').value)
Or you can attach an event handler to get the date whenever it changes
<script type="text/javascript">
$("#datetimepicker3").on("dp.change", function (e) {
let date = e.date;
console.log(date);
// no need to convert to a date
});
</script>
You can simple get that in different variable using various methods that are provided by JavaScript to get everything.
You can do is(id it's a date object else you have to convert it to date object)
let startDate = new Date(innerDate).getDate() + "/" + new Date(innerDate).getMonth() + "/" + new Date(innerDate).getFullYear();
let startTime = new Date("2021/07/07 14:00").getHours() +":"+ new Date("2021/07/07 14:00").getMinutes()
If you want something else you can retrieve that too with methods such as milliseconds,UFC time etc.
If you find this messy I would suggest you to use moment.js.
https://momentjs.com/

How to get Timezone by passing abbreviation?

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');

Getting standard IST time

I am trying to get current Standard IST time but I am getting system time only using below function.
var currentTime = new Date();
var currentOffset = currentTime.getTimezoneOffset();
var ISTOffset = 330;
var d1 = new Date(currentTime.getTime() + (ISTOffset + currentOffset)*60000);
Is there any api or library that helps me in getting standard IST time.?
You could use moment.js, a standard utility to handle all time and date related functions. You simple import moment.js and for your desired use case implement a method such as
moment().utcOffset("+05:30").format()
for setting the the time in IST which is +05:30 from GMT.
Java script currently doesn't helps to get current time, I have finally used the timestamp in my response headers in service calls

How can I get date and time separately using jQuery?

In my table date is listed as "2015-07-31 06:02:20". How can I get date and time separately using jQuery?
I used some code but it shows some errors.
var timestamp = new Date(data.responsecusthistory.created_at);
var date = timestamp.toDateString();
Error: Invalid Date
var date = data.responsecusthistory.created_at.split(" ")[0];
var time = data.responsecusthistory.created_at.split(" ")[1];
If you want to have a time string (i.e. HH:MM:SS), try e.g. this:
var timestampTime = timestamp.toTimeString().split(' ')[0];
Anyway, there's no obvious reason why you get the "Error: Invalid Date", as long as
data.responsecusthistory.created_at
is a correct value, such as "2015-07-31 06:02:20". Please consider double-checking this variable.
Try this:
dateString= "2015-07-31 06:02:20";
temp = new Date(dateString);
dateStr= $.datepicker.formatDate('mm/dd/yy', temp );
for getting different formats check the link https://github.com/phstc/jquery-dateFormat.
Using different formats we will get different date, time etc in which ever forms we need it.

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

Categories

Resources