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
Related
I have the following timezones:
when a user selects a timezone from the list how do i get whats the current time and date at that timezone? for example if i choose "Azores" i want to get the current date time for that area.
In moment i could use IANA timezones and pass the timezone like and format it to ("dddd, D MMM YYYY" )
moment().tz().guess;
but the user doesnt want to use IANA Timezones because they cant find some of the countries so i cant go with that option.
Is there a way i can determine the current datetime based on the timezones specified?
i also tried the following if i passed in the offset but that didnt work example:
moment().utcOffset("-01:00");
because its taking my local time and offsetting it so it doesnt give me the actual current datetime.
To get all available timezones:
How to get list of all timezones in javascript
const arr = Intl.supportedValuesOf('timeZone');
JS Date object is in UTC, so to get Date with timezone offset you can use
I want to get New York time in this JavaScript clock?
const date = new Date(new Date().toLocaleString("en-US", {timeZone: "America/New_York"}));
I have to convert local date and time to utc format.
Therefore if I have date as 2021-08-11 (YYYY-MM-DD) and time as 2:40 PM, then slot date time should be 2021-08-11T09:10:00.000Z.
I have tried multiple things, but failed
const dateTimeInUTC = moment(
`${formattedDate} ${formatTime}`,
'YYYY-MM-DD HH:mm:ss'
).toISOString();
above code resulted me => 2021-08-10T21:10:00.000Z (which is +5:30 more)
Also, tried following
const formatted = formattedDate + " " +formatTime (2021-08-11 02:40 PM)
const result = new Date(formatted).toISOString();
this gave me
Range error :Invalid Date
However, this works as expected in console, but gives error in mobile.
I tested it now, convert - to / in date format then it will work fine in react native both on browser console and mobile. for more info you can check that link
var isoDateString = new Date('2021/08/11 12:00 pm').toISOString();
console.log(isoDateString);
If you want to use your date format (date witn -) then you to add T instead of space and the time should be on 24 hour scale
like
var isoDateString = new Date('2021-08-11T13:00').toISOString();
this solution will also work for you.
thanks
you can try that
var isoDateString = new Date('2021-08-11 2:40').toISOString();
console.log(isoDateString);
If you want to covert current System Time to UTC then Do :
const dateTimeInUTC = new Date().toUTCString();
console.log(dateTimeInUTC);
Or if you want convert any specific Date-Time to UTC then Do :
const dateTimeInUTC = new Date("October 13, 2000 00:45:00").toUTCString();
console.log(dateTimeInUTC);
I am making a filter between two dates, Start date and End date, the filter works perfect, it brings the data but it does not bring the complete data and it is because when selecting the dates and converting them to the format, it converts them but one day remains.
This way I am converting the dates:
FiltrarPorFechas(incial, final) {
this.ListaUsuarios = [];
const IniDate = new Date(incial);
const EndDate = new Date(final);
}
associate an image with debug and conversion:
As shown in the image, the initial and final dates arrive at the method thus "2019-07-10" and "2019-07-31" but when I try to convert them it puts them one day less as shown in the image.
I have tried to use moment formatDate and it does not work, I do not understand why and I do not want to add one day.
Somebody could help me ?
You may be potentially having an issue with Timezone. You could reset the time to 00:00:00 either using moment.js or from a normal Date() object.
Now, since your aim is to compare the times, you can use the diff() available from moment.js to achieve this. Please find the sample code below
(function() {
initial = '2019-07-09';
initial_formatted = moment(new Date(`${initial} 00:00:00`));
final = '2019-07-31';
final_formatterd = moment(new Date(`${final} 00:00:00`));
console.log(initial_formatted.diff(final_formatterd, 'days'));
// moment(new Date(`${incial} 00:00:00`)).format('YYYY-MM-DD HH:mm:ss');
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
More info
Moment diff()
Date Object set() method
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?
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();