I have attribute string Time:"10:50" response in JavaScript.
How to compare only hour of string with hour of system.
E.g: if 10=10 then run code in function.
Please help. thanks
You should wrap the string into a date object and use the getHours() function on the date object. something like
//in the oldDate, pass the string as params to the date constructor
let oldDate = new Date("date string")
//construct this one without a parameter and it will use the system time
let systemDate = new Date()
if(oldDate.getHours() == systemDate.getHours())
But if you are quite certain that the response is of the form "10:50" then this will be the better option
let time = "10:50"
let h = parseInt(time.split(":")[0])
let systemDate = new Date()
if(h == systemDate.getHours())
Create a Date object: let dateTime = new Date();
Get the current hour: let hour = dateTime.getHours();
Compare it with your desired object.
Related
I know the data before sending will be parsed to string from any type. So where is this process done and how does it happen?
I had this question when I had a problem regarding the format for a Date type in javascript. Specifically, I want to send data that has a field of type Date and I want to format it before sending (example: 29-12-2022). However, I always get a result like 2022-12-29T17:00:00.000Z.
I can convert the interface of data from Date to String. However, I don't want to do that
You should use functions provived default by Date class:
Use toLocaleDateString function
const d = new Date();
let text = d.toLocaleDateString();
console.log(text);
Manually concatenate date, month, year strings:
let d = new Date();
let mm = d.getMonth() + 1;
let dd = d.getDate();
let yy = d.getFullYear();
console.log(`${dd}-${mm}-${yy}`);
I have a ngbDatePicker which helps me to pick a date. Then it returns an object like this:
{year:2020,month:12,day:03}
I'd like to get an ISOString of this date with today's time(current). So if time is 18:42 I should be able to get something like this:
2020-12-03T18:42:00.000Z
To do that I parsed object and made date firstly
(model is the object holds date like above)
var date = new Date(this.model.year + "-" + this.model.month + "-" + this.model.day);
//then to add today's time I found solution below on the internet whcih didn't work for me
var date2 = new Date(date);
var isoDateTime = new Date(date2 .getTime() - (date2 .getTimezoneOffset() * 60000)).toISOString();
Here isoDateTime returns 2020-12-10T03:00:00.000Z which is not I want.
How to solve this?
Working stackblitz
Just take the time part of a Date object and combine it with this.model:
var date2 = new Date();
var date = new Date(this.model.year, this.model.month-1, this.model.day,
date2.getHours(), date2.getMinutes(), date2.getSeconds());
var isoDateTime = date.toISOString();
console.log(isoDateTime);
The month parameter is 0 based, so we have to substract 1 from the month.
Result (I chose Dec.1st 2020 in the Datepicker):
2020-12-01T19:22:42.000Z
Try on Stackblitz
You can create a single Date for the time and append it to values from the object:
function myISOString(obj) {
let z = n=>('0'+n).slice(-2);
return `${obj.year}-${z(obj.month)}-${z(obj.day)}T${new Date().toTimeString().substring(0,8)}`;
}
let obj = {year:2020, month:12, day: 3};
console.log(myISOString(obj));
PS the use of leading zeros like 03 for numbers should be avoided as once upon a time that notation indicated octal values (but not any more), so 09 might be confusing.
I was trying to convert a Number to Date in Javascript. Below is the code which I have tried
var newDate = new Date(1012256900000);
console.log("Test: ",newDate.toString('MMM-yyyy'));
This is working fine.
But when I get it from $rootScope object, am getting invalid date :(
var newDate = new Date($rootScope.lastLoginTime);
console.log("Test: ",newDate.toString('MMM-yyyy'));
This is printing Invalid Date. FYI,
$rootScope.lastLoginTime = 1463256900000;
Gonna take a guess here, but look at what I did in the browser console:
new Date(1463256900000)
> [date] Sat May 14 2016 21:15:00 GMT+0100
new Date("1463256900000")
> [date] Invalid date
Completely wild guess, but perhaps you should ensure you are passing a number, not a string, to new Date() - the constructor behaves very differently in either case.
Consider instead trying this:
var newDate = new Date();
newDate.setTime($rootScope.lastLoginTime);
setTime takes a numeric argument only, and will convert your string to a number for you if you pass it one.
Guess your $rootScope.lastLoginTime = '1463256900000'; than $rootScope.lastLoginTime = 1463256900000;.
new Date(time) does works when time is number , not when time is string.
var t1 = 1463256900000;
var t2 = '1463256900000';
var d1 = new Date(t1);
var d2 = new Date(t2);
console.info(d1);
console.info(d2);
I'm pulling some data from two different APIs and I want to the objects later on.
However, I'm getting two different date formats: this format "1427457730" and this format "2015-04-10T09:12:22Z". How can I change the format of one of these so I have the same format to work with?
$.each(object, function(index) {
date = object[index].updated_at;
}
Here's one option:
var timestamp = 1427457730;
var date = new Date(timestamp * 1000); // wants milliseconds, not seconds
var dateString = date.toISOString().replace(/\.\d+Z/, 'Z'); // remove the ms
dateString will now be 2015-03-27T12:02:10Z.
Try moment.js
var timestamp = 1427457730;
var date = '2015-04-10T09:12:22Z';
var m1 = moment(timestamp);
var m2 = moment(date);
console.log(m1);
console.log(m2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
You can use .format() method in moment to parse the date to whatever format you want, just like:
m2.format('YYYY MMM DD ddd HH:mm:ss') // 2015 Apr 10 Fri 17:12:22
Check out the docs for more format tokens.
What you probably want in javascript, are date objects.
The first string is seconds since epoch, javascript needs milliseconds, so multiply it by 1000;
The second string is a valid ISO date, so if the string contains a hyphen just pass it into new Date.
var date = returned_date.indexOf('-') !== -1 ? returned_date : returned_date * 1000;
var date_object = new Date(date);
Making both types into date objects, you could even turn that into a handy function
function format_date(date) {
return new Date(date.indexOf('-') !== -1 ? date : date * 1000);
}
FIDDLE
Take a look at http://momentjs.com/. It is THE date/time formatting library for JavaScript - very simple to use, extremely flexible.
Is there a way I could get the year, month (0 based) and day from '03/05/2013'
If so, how?
Thanks
Is there a safe way to do it that can check if it is in the correct format?
You have the Date.parse method which parses a Date string and returns its timestamp, so you can call new Date().
Something like this:
new Date(Date.parse('03/06/2013'))
Most easy is using the split() function, i think:
var date = "03/05/2013";
var dateParts = date.split("/");
var day = dateParts[0];
var month = dateParts[1];
var year = dateParts[2];
http://jsfiddle.net/s7ma2/1/