Parsing Time Variabale - javascript

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>

Related

How to format date with date-fns?

I've been trying to format a date using date-fns but I keep failing. Basically I have it working just fine with momentJS but not with date-fns:
Here's my date:
"10-13-20" // month, day, and year
Now with momentJS it works just fine like this:
let result = moment("10-13-20", 'MM-DD-YY').format()
// result = "2020-10-13T00:00:00-06:00"
So I'm trying to do the same using date-fns but no luck. Can anyone point me in the right direction? Thanks in advance!
let result = format(new Date("10-13-20"), 'MM-DD-YY') // Not working
As you can see, with moment lib, we need 2 steps to get the result: parse string to Date object, then format date object to string.
Your code - format(new Date("10-13-20"), 'MM-DD-YY') is format step, try convert a date object to a string with format template is MM-DD-YY. But your date object is not correct.
The solution is doing the same with moment lib:
Parse date string to date object. Use parse
const dateString = '10-13-20';
const date = parse(dateString, 'MM-dd-yy', new Date()) // not MM-DD-YY
Format date object to result string. Use format
const result = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
console.log(result)
Result will be like (the same with moment's result in my timezone):
2020-10-13T00:00:00.000+09:00
const date = "2021-12-20"
console.log(format(parseISO(date), "dd-MM-yyyy"));
// output: 20-12-2021

Formatting a Date String into a Date

I want to format a string Date "14/06/2019 12:52" using moment library in order to manipulate a Date object and not a string.
I tried using moment library but didn't succeed to have a Date object
const myDate = moment('14-06-2019 12:52')
the const myDate is not of type Date.
You need to provide the format also
const myDate = moment('14-06-2019 12:52', 'DD-MM-YYYY hh:mm');
You could use moment().toDate();
Refer here for more info http://momentjs.com/docs/#/displaying/as-javascript-date/

How to add days to current date in angular

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]);

What is the right way to pass a date as a string in a specific format and return a date object with momentjs?

I have been looking at the momentjs momenjs documentation because I want to use it to return a date object from a date that is a string in a particular format. My code looks like this.
const date = moment(stringDate, 'YYYY-MM-DD HH:MM:SS');
console.log(date);
But it creates an invalid date as can be seen here.
What am I doing wrong. How can I get a date object from a date string that is in a particular format?
You're using MM:SS for minutes:seconds, but it should be mm:ss; details here.
Example:
const stringDate = "2018-05-11 14:25:37";
// Parsing
const m = moment(stringDate, 'YYYY-MM-DD HH:mm:ss');
// Show it in a different format to demonstrate parsing worked
console.log(m.format("DD/MM/YYYY HH:mm"));
// Accessing the underlying Date object
const dt = m.toDate();
// Log that dateobject
console.log(dt);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
In order to parse a string then return a different format you can:
> moment(dateString).format('DD.MM.YYYY')
Further examples:
Official docs: https://momentjs.com/docs/
Other sources: https://coderwall.com/p/vc3msa/a-very-short-introduction-to-moment-js

How to parse ISO 8601 into date and time format using Moment js in Javascript?

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/

Categories