Getting only hours and minutes from date object [duplicate] - javascript

This question already has answers here:
How do I use .toLocaleTimeString() without displaying seconds?
(13 answers)
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 3 years ago.
I want to get a JavaScript code that displays only hour, minute and AM or PM. I don't want seconds.
This is the code i have so far:
var d = new Date();
document.getElementById("time").innerHTML = d.toLocaleTimeString();

Date objects have getHours() and getMinutes() methods.
getHours() returns values in the range of [0, 23], meaning you can compare with 12 to determine 'am' or 'pm'.
Lastly, consider using mdn as a helpful reference about the standard API.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
let d = new Date('December 17, 1995 22:11:51');
let pm = d.getHours() >= 12;
let hour12 = d.getHours() % 12;
if (!hour12)
hour12 += 12;
let minute = d.getMinutes();
console.log(`${hour12}:${minute} ${pm ? 'pm' : 'am'}`);

Related

Subtracting time from current time [duplicate]

This question already has answers here:
How can I subtract hours from a HH:MM AM time string in Javascript?
(7 answers)
Closed 1 year ago.
I have 2 strings, that have the minute and hour that a function needs to occur at.
I want to check, if the minute and hour which are specified in string format, and from my database are within 5 minutes of the current time, call this function.
My original thought was something like:
(today.minute is the current minute)
today.minute:
minute = "55"
hour = "14"
var today = new Date();
var time = today.getMinutes(), today.getHours()
if (today.getMinutes() - 5 == minute) {
myFunc()
}
But that isn't going to work, because I need the hour and minute - 5 minutes... how can I do this?
Is this what you are looking for?
let minute = "55"
let hour = "14"
let today = new Date();
//var time = today.getMinutes(), today.getHours()
if (Math.abs(today.getMinutes() - Number(minute)) <= 5) {
myFunc()
}
function myFunc() {
console.log('myFunc called', today.getMinutes());
}

How to convert array of time to 24 hour format [duplicate]

This question already has answers here:
convert 12-hour hh:mm AM/PM to 24-hour hh:mm
(38 answers)
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
How to force addition instead of concatenation in javascript [duplicate]
(3 answers)
Closed 2 years ago.
So I have a time array which holds slot time. I am trying to convert 12 hr format to 24 hr format but it is not working
Here is what I have tried so far:
let timeArray = ["11:12 AM", "11:13 AM", "1:14 PM"];
for (i in timeArray) {
let [time, mod] = timeArray[i].split(" ");
let [hr, min] = time.split(":");
if (hr < 12) {
hr = hr + 12;
}
console.log(hr);
}
Here is the output:
The expected output should add 12 to hr number to convert it to 24 hr format.
I would suggest you use moment.js ( https://momentjs.com/ )
You can play with date and time object in numerous way you want by using moment.js
Use parseInt to convert the string to an integer.
let timeArray = ["11:12 AM", "11:13 AM", "1:14 PM"];
for (i in timeArray) {
let [hr, min] = timeArray[i].split(":");
if (hr < 12) {
hr = parseInt(hr) + 12;
}
console.log(hr);
}

How to get the date in days ago in javascript [duplicate]

This question already has answers here:
How to calculate number of days between two dates?
(42 answers)
Closed 3 years ago.
So,
I have this
10 Dec, 2019T14:07:21
format of date coming from backend , what I need is to find how many days ago.
like today is 20 , so for todays date it will give 0 days ago.
You can try this code.
var date1 = new Date("12/13/2010");
var date2 = new Date("12/20/2010");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24), 10);
console.log(diffDays )
You can compare two dates. Substracting them will give you the miliseconds difference. THose miliseconds can be converted into days.
const now = new Date();
// Mimick a backend date
const daysAgo = new Date();
daysAgo.setDate(daysAgo.getDate() - 10);
// Compare both, outputs in miliseconds
const diffMs = now - daysAgo;
// Get the number of days by dividing by the miliseconds in a single day
const daysDiff = Math.round(diffMs/(1000*60*60*24));
console.log(daysDiff)
(new Date()-new Date("10 Dec, 2019T14:07:21".replace("T"," ")))/1000/60/60/24
What you try to do is called "date diff" meaning you want to find the difference in days between 2 dates. First of all you need to create a new Date object from the string you want. You can do that using Moment.js library that will parse your date string and return a Date object.
var date1 = moment("10 Dec, 2019T14:07:21", "DD MMM, YYYY");
var date2 = new Date() //today date
A simple js function accomplishing that is the one below
function dateDiff(date1, date2) {
var datediff = date1.getTime() - date2.getTime();
return (datediff / (24*60*60*1000));
}
dateDiff function will return the difference between dates, so if date1 is a previous day, it will return a negative number of days. In order to convert it to the "x days ago" format you need, you need to simply multiply the result by -1.

Need just time from this date time format [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I was looking for EDT time and got this in this format :
Wed Nov 01 2017 06:42:26 GMT+0530 (India Standard Time)
Although, I just want it like 6:42 AM to display !! I tried date time function
but doesnt help !!
Is it advisable to do this with any string function or will it give issue in future (I dont want any calculation on this )
Use moment.js format to work with date in JS.
moment("Wed Nov 01 2017 06:42:26 GMT+0530").format("hh:mm a")
You can use
var date = "Wed Nov 01 2017 06:42:26 GMT+0530";
var formattedDate = new Date(date);
var hours = formattedDate.getHours();
var minutes = formattedDate.getMinutes();
hours = (hours > 12) ? (hours - 12) : hours;
var period = (hours > 12) ? 'PM' : 'AM';
var time = hours + ':' + minutes + ' ' + period;
or you can also use moment.js, here is the details on 'format' function.
var time = moment(date).format("hh:mm A");
var date = new Date();
var hour = date.getHours();
var minutes = date.getMinutes();
var time = hour + ':' + minutes;
This would work.
Note: momentjs is great library for time manipulation, it is not good idea to include such a big library for small functions like this.

Get previous month date javascript [duplicate]

This question already has answers here:
How to get 30 days prior to current date?
(16 answers)
Closed 7 years ago.
How can I get previous month date in javascript. Suppose you have today's date like:
var abc = new date();
It will return today's date for example 03-11-2015. Now I want to get 03-10-2015. This is 30 days less than todays date. How can I do this?
var d = new Date();
d.setMonth(d.getMonth() - 1);
Check out momentjs, great little library for manipulating and formatting dates.
Complementing Robert Shenton's answer:
var d = new Date();
var newMonth = d.getMonth() - 1;
if(newMonth < 0){
newMonth += 12;
d.setYear(d.getFullYear() - 1); // use getFullYear instead of getYear !
}
d.setMonth(newMonth);

Categories