I am trying to compare a couple of dates in javascript. First of it, I got froma database. In database it is in date format but after send it in a json file I guess it is just a string.
The second I got it in Date format in javascript. I found this script in javascript:
var todayAEJ = new Date();
var ddAEJ = todayAEJ.getDate();
var mmAEJ = todayAEJ.getMonth()+1; //January is 0!
var yyyyAEJ = todayAEJ.getFullYear();
if(ddAEJ<10){ddAEJ='0'+ddAEJ} if(mmAEJ<10){mmAEJ='0'+mmAEJ} todayAEJ = ddAEJ+'-'+mmAEJ+'-'+yyyyAEJ;
ANd it works like a charm.
The other date is like this: 13-01-2014
I tried to compare like this:
if(todayAEJ > val.date_End)...
But it returns true when today's day is bigger than val.date_End's day. So I cannot use this form when the month is diferent. What can I do in this case?
otherDate = '13-01-2014';
myDate=myDate.split("-");
var newDate=myDate[1]+","+myDate[0]+","+myDate[2];
otherDateTimeStamp = new Date(newDate).getTime();
todayAEJTimeStamp = new Date().getTime();
if(todayAEJTimeStamp > otherDateTimeStammp){
// your code
}
you can even use var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
Use above code...it will solve your problem!
Iinjoy...
If you can use MM-DD-YYYY format instead of DD-MM-YYYY you can use Date.parse:
var todayAEJTimeStamp = Date.parse(todayAEJ);//Date parse needs `MM-DD-YYYY` format
//if val.date_End is instance of Date
if(todayAEJTimeStamp > val.date_End.getTime()) {
//your code here
}
You have to compare them via microtime
var date = new Date("11/21/1987 16:00:00");
var milliseconds = date.getTime(); // Compare this milliseconds
Apart from timestamp solutions, I would recommend to use date.js in any case you want to play with Date object in JS. Date object has pretty odd behaviour in JS, and this library comes in handy.
Related
I am trying to display the date from time stamp using JavaScript but not working please check my code and it's not working due to string time but if i passed in number then it's working but this time coming from API so i must need to do something here. can anyone please help me.
var timestamp = '1607110465663'
var date = new Date(timestamp);
console.log(date.getTime())
You can convert string to number using methods such as Number or parseInt before creating Date object.
e.g.
var timestamp = '1607110465663'
var date = new Date(Number(timestamp));
console.log(date.getTime())
Parse string into int then...:
var timestamp = '1607110465663'
var date = new Date(parseInt(timestamp));
and use date object
console.log(date)
Remove the '' from timestamp
var timestamp = 1607110465663
var date = new Date(timestamp);
console.log(date.getTime())
console.log(date)
See JSfiddle
the user provide value is
var date = 2019/11/06 22:59:50;
but i want to convert the data of date as
date = 55;
from ("YYYY/MM/DD HH:mm:ss") to ("mm")
having only the minute so that i can subtract the data as alert(date - Date().getMinute()) so that i can save the value to a variable for conditions
Date manipulation is really rough in JS. If you can ensure the format is going to be exactly like that, you can do something like this:
var date_raw = "2019/11/06 22:59:50";
var date = new Date(date_raw);
var mm = date.getMinutes();
Otherwise, I suggest you look into moment.js if you need something more complex.
Edit: if you're just looking for help on subtracting time from a date object, that's already answered
If I understand what you want, in first place I would transform the date var into a Date object, like this: var date = new Date("2019/11/06 22:59:50");. Then I'd add a min var that would take the mm part of the date like this: var min = date.getMinutes();. That would do the job.
Using this:
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
I get a date and time like this 2018-12-30T14:15:08.226Z, but only i want is this 2018-12-30. How can I retrieve just the date?
**This is Fixed. Thank You everyone who helps!!!
You're experiencing a JS problem, it has nothing to do with Angular.
This will use Date methods to get all the data you want:
const date = new Date ();
let dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
// 2018-12-26
You can take advantage of the fact that the ISO 8601 format (what you are getting by implicitely converting to string) is well-codified with a separator T between the date and time in order to split it.
toISOString() gives you what you're seeing. split("T") splits the string into an array of strings with T as separator. [0] then extracts the first element.
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
console.log(myDate.toISOString().split("T")[0]);
This date issue is giving me a hard time. i am getting the below value from sql table.
var CurrentDate = 2017-04-25T00:00:00
I need to convert this to 04/25/2017 at the UI. I tried using various date methods but didnt get wat i want. Can someone pls shed some light. this is wat i tried.
var date_new = new Date(CurrentDate);
date_new= date_new.toLocaleDateString();
Reference: MDN
var CurrentDate = "2017-04-25T00:00:00";
var date_new = new Date(CurrentDate);
document.write(new Intl.DateTimeFormat('en-US').format(date_new));
You could use momentjs to parse your dates and times, it's very easy and powerful.
Install moment and then require it in your js.
const moment = require('moment')
const currentDate = '2017-04-25T00:00:00'
const parsedCurrentDate = moment(currentDate).format('l')
And you will get: '4/25/2017' stored in your parsedCurrentDate variable.
Just show that in your ui.
There are other formatting options, just take a look at the moment's website.
You are almost there, just surround your date in quotes.
var CurrentDate = '2017-04-25T00:00:00';
I have a date as 2014-12-03
I need to convert this to DD-MM-YYYY
I have tried as
var from = "2014-12-03";
var numbers = from.match(/\d+/g);
var date = new Date(numbers[2], numbers[0]-1, numbers[1]);
alert(date);
This is my jsfiddle
http://jsfiddle.net/bnz9d6gz/
Could anybody please help me .
Cut your date string into 3 pieces, reverse order, and glue it back
"2014-12-03".split('-').reverse().join('-'); // "03-12-2014"
Breakdown:
"2014-12-03" - string
['2014','12','03'] - create an array by splitting each string which isn't -
['03','12','2014'] - reversing the array
"03-12-2014" - joining all items in the array and putting - between them.
How about this:
var from = "2014-12-03";
function convert(date){
var d=new Date(date);
function addLeadingZero(d){
return (d<10)?"0"+d:d;
}
return [d.getDay(), d.getMonth()+1, d.getFullYear()].map(addLeadingZero).join("-");
}
console.log(convert(from));
Fiddle updated.
If I were you I'd use momentJS, it'll handle this kind of thing for you. If you pull in the library all you'll need to do is:
var from = moment("2014-12-03", "YYYY-MM-DD");
var date = moment.format("DD/MM/YYYY");
alert(date);
Much easier than trying to roll your own solution and if you ever need to support additional formats it'll make your life easy.