Convert Time to Long format in AngularJS - javascript

I have an input box where I am displaying the time in the following format: HH:mm AM/PM.
<input type="text" ng-model="myTime" required/>
I am doing this using the date filter in AngularJS -
$scope.myTime = $filter("date")(new Date(), 'shortTime');
I want to pass this time to rest layer, which needs this time in the long format. Is there any way to do that in Angularjs?
PS- I would like to do it without using Moment.js, if possible.

Well, lets imagine, you have a string, containing "HH:mm" (btw, HH means that your hours range from 0-23, so ap/pm is kinda redundant there). To get your local date with that time into the long format you do:
// parse the time string
var time = "22:30".split(":")
// create a new date
var x = new Date();
// adjust time to target time
x.setHours(time[0]);
x.setMinutes(time[1]);
// finally, get the long version
x.getTime();

Related

Can you convert results from input type date and input type time to seconds since epoch

I am making a routing system and want to add a function so you can enter a start date and time through an HTML form, then JavaScript will add the route time to it and display it on the page.
However I'm having a bit of trouble with getting the date and time in the right format.
You can get the time in seconds with this:
var myDate = new Date();
myDate.getTime() / 1000;

How to get hour from datetime-local?

I'm trying to get the hour in different ways from a datetime-local input but every time I've got the output as "invalid" date.
So I would like to know how can I get the hour from a datetime-local input using jquery or js.
Here is what I've tried:
var fine = moment($('#datafine').val).format("HH");
And without moment that was something like
var datafine = new Date($('#datafine').val);
When you get the value from the input, it is converted to a standard format, regardless of the displayed format:
One thing to note is that the displayed date and time formats differ from the actual value; the displayed date and time are formatted according to the user's locale as reported by their operating system, whereas the date/time value is always formatted yyyy-MM-ddThh:mm.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
So you can use this with new Date(val).getHours() to get the hours.
Using jQuery:
var hour = new Date($('input[type="datetime-local"]').val()).getHours()
console.log($('input[type="datetime-local"]').val(), hour)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="party">Enter a date and time for your party booking:</label>
<input id="party" type="datetime-local" name="partydate" value="2019-06-01T19:30">
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
If I understand correctly what you're trying to do, something like this may help. You would just need to add in the date value from your input into new Date(). The code below would give you the hours in the specified locale.
const date = new Date();
const options = { hour12: false, hour: 'numeric'};
const hours = date.toLocaleTimeString('en-US', options);
console.log(hours)
Date.prototype.toLocaleTimeString() on MDN

How the .getTime() can preserve the original date dd/MM/yyyy?

I have a trouble. I have an angular Javascript application that is storing the dates in mysql with milliseconds, but in some moments the date is interpreted with one more or less day. For example:
If the user selects in the date picker: 03/02/2017, the application is saving this (in milliseconds) as 02/02/2017 or 04/02/2017. I believe it is due to the timezone. This is the way I'm using to convert the date in milliseconds:
var temp = $("#datetimeField").val().split("/");
var newDatetime = new Date(temp[2], temp[1] - 1, temp[0]).getTime();
As you can see, I know the day, the month and the year before store it in the database. Normally the date is stored and works well, but in some moment the date changes as I showed above. How can I always get temp[2]/temp[1]-1/temp[0] ??? from the stored milliseconds?

parsing a UTC ISO date to a local time date in javascript/jquery

I have tried to search for the answer already, and although I find answers that are very similar, I don't think they are exactly what I am looking for. Please forgive me if this is already answered elsewhere.
I am trying to parse an ISO date in javascript so that I can compare it to the client system date and display information depending on if the ISO date is before or after the client system date.
This was fine until I needed to support IE8 and now I am stuck.
I have created a function because I have three different dates that I need to do this to.
for example, my ISO date is: 2015-12-22T11:59 in UTC time.
but once my date is parsed, the full date is 11:59 in local time, no matter which time zone i test, it's always 11.59 in THAT time zone.
I know that the function I have created currently doesn't do anything with timezone, this is where I am stuck. I don't know what to add to get my end date to change as a reflection of the timezone of the clients machine.
any help or advice would be greatly appreciated.
I am not able to use something like moments.js because I have an upload restriction.
Jquery is available though. or plain javascript.
<script>
function setSaleContent() {
//creating a new date object that takes the clients current system time. so we can compare it to the dates stored in our array
var currentDate = new Date();
console.log(currentDate + " this is the clients date ");
//These variables actually come from an external array object, but I'm putting them in here like this for this example.
var destinations = {
freedate: "2015-12-16T11:59",
courierdate: "2015-12-22T11:59",
nextdaydate: "2015-12-23T11:59",
}
//fetch all the ISO dates from the array.
var freeDateISO = destinations["freedate"];
var courierDateISO = destinations["courierdate"];
var nextdayDateISO = destinations["nextdaydate"];
//I am creating this reusable function to split up my ISO date for the sake of IE8.. and create it into a date format that can be compared against another date. I know that this isn't doing anything with my timezone and that is where my problem lies.
function parseDate(str) {
var parts = /^(\d{4}).(\d{2}).(\d{2}).(\d{2}):(\d{2})/.exec(str);
if (parts) {
return new Date(parts[1], parts[2] - 1, parts[3], parts[4], parts[5]);
}
return new Date();
}
//I would like this date to change to reflect the time zone of the clients system time.
//currently returns the date at 11.59 regardless of timezone.
//If i was in GMT i would want it to say 11.59
//If i was in CT time I would like this to say 05.59
//If i was in Perth I would like this to say 19:59
var freeDate = parseDate(freeDateISO);
console.log(freeDate + " this is the converted date for IE")
}
window.onload = setSaleContent;
The simple solution is to append Z to the ISO date to indicate it is in UTC time, such as 2015-12-22T11:59Z.
When JavaScript parses that date as a string, it will then automatically convert the UTC date to the local time zone.
While this is simple enough with a parsing call in the form new Date(str);, it will not play nice with your parse call with numerical arguments targeting IE8 and other old browsers.
A polyfill for parsing ISO dates with timezone exists: Javascript JSON Date parse in IE7/IE8 returns NaN
This can replace your custom parseDate function after some modification to take an input string.
Alternatively, implement your own custom date manipulater to account for the local timezone using the .getTimezoneOffset() method on the newly created date, which gives the time zone offset in minutes, but you will have to come up with a method of utilising the offset such as adjusting hours and minutes, due to the limited methods of the JavaScript date object.

parse time in dateBox(), or eliminate time in datebox

Is there a way to parse the time in dateBox(), or better yet eliminate it all together? I want someone to be able to use the dateBox() to choose the date only, but want to get rid of the time and use another function I create for the time.
thanks
one simple way of doing this is like this :
var date = new Date(e.parameter.start);// convert the string to a full date object (with time value) (the date widget is named 'start' in this example)
var dateOnly = date.setHours(0,0,0,0); // set time to '0' (hours,min,sec,millisec)or use the hours and minutes you get from elsewhere...
You can have a look at this example sheet where I use this to combine data from 2 columns (date and time) to create a complete date object

Categories