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';
Related
I am looking for to convert this date and time to relative time like twitter have ex. 5 sec ago or 1 month ago.
I have stored date and time in different column of database.
i am executing this in JavaScript, i tried Momentjs but it does not work well or i may not understand its format or flow of work.
i am little new to javascript, i started learning react a week ago
from database:
date =>31.03.2019 time=>16:06:05
Thank You For Help.
Success Update :
Mission was to convert "31.3.2019 19:45:55" this date into a time like twitter or facebook have.
let date = new Date()
var then = request.date +' '+ request.time;
var ms = moment(date,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD.MM.YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
var timeAgo = d.humanize();;
In moment we can define the format of date we are passing and with humanize function its easy to get the desire output.
What i didnt know was the definition of format of date we pass. #NOW I AM CLEAR SIR.
Thank You For Helping Everybody. Thank You For Sharing .
You can use moment js
let date = new Date()
console.log(moment(date).fromNow())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I have a time stamp that i'm using for the x-axis of my line charts, as of now i'm just passing it as "2018-11-29T20:32:24.025Z" I would like to clean this up to only display the day, hour, and minute. It looks like Moment.js is a popular library for this, but i'm having some problems formatting it.
Failed Attempt
let parsedDate = moment(parsedData.published_at,'DD--hh--mm')
You need to
Parse the date
let parsedDate = moment(parsedData.published_at);
Format the date
let formattedDate = parsedDate.format(...
See the documentation.
If I understand your question correctly then you can pass your inputDate (ie 2018-11-29T20:32:24.025Z) directly to the moment constructor.
Doing this will give you a moment object for that inputDate string, and from that you can call format() with your DD--hh--mm pattern to format the required string:
let inputDate = "2018-11-29T20:32:24.025Z";
// Create a moment object from input string
let parsedDate = moment(inputDate);
// Format a string from moment, based on the required pattern
let formattedData = parsedDate.format('DD--hh--mm');
console.log(formattedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
I am currently using Moment js to parse an ISO 8601 string into date and time, but it is not working properly. What am I doing wrong? And I would take any other easier solutions as well.
The ISO 8601 I would like to parse: "2011-04-11T10:20:30Z" into date in string: "2011-04-11" and time in string: "10:20:30"
And tried console.log(moment("2011-04-11T10:20:30Z" ,moment.ISO_8601)) and console.log(moment("2011-04-11T10:20:30Z" , ["YYYY",moment.ISO_8601]) as a test, but it just returns an object with all different kinds of properties.
With moment.js
var str = '2011-04-11T10:20:30Z';
var date = moment(str);
var dateComponent = date.utc().format('YYYY-MM-DD');
var timeComponent = date.utc().format('HH:mm:ss');
console.log(dateComponent);
console.log(timeComponent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
Or simply with string manipulation
var str = '2011-04-11T10:20:30Z';
var parts = str.slice(0, -1).split('T');
var dateComponent = parts[0];
var timeComponent = parts[1];
console.log(dateComponent);
console.log(timeComponent);
There's two parts to the moment operation: reading the date/time in, and spitting it back out. You've got the first part:
moment("2011-04-11T10:20:30Z")
but then you need to call an output function, eg:
moment("2011-04-11T10:20:30Z").format('YYYY-MM-DD h:mm:ss a')
EDIT: However it's worth noting that Moment itself is a deprecated library, and even its authors will recommend switching to a smaller competitor: https://momentjs.com/docs/
I would like to format a date using moment JS which I get the date from js timezone library. anybody to help.
Following the code, I have written:
var tz = jstz.determine();
var timezone =tz.name();
var timess = moment().tz(timezone).format();
var times_new = moment(timess).format("YYYY-MM-DD HH:mm");
Also I have include jstz.min.js and moment-with-locales.min.js
To use moment time zones, you need to add the Moment Time Zone library.
After that you can use the moment tz method. You can avoid the two passages:
var timess = moment().tz(timezone).format();
var times_new = moment(timess).format("YYYY-MM-DD HH:mm");
And write all in one line:
var timess = moment().tz(timezone).format("YYYY-MM-DD HH:mm");
Because moment().tz(timezone) returns a moment object.
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.