This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
Closed 1 year ago.
receiving string of date that looks like this "/Date(1633421520000)/",
with moment I could just use it as moment("/Date(1633421520000)/") whats the equivalent for date-fns ?
for example differenceInMilliseconds how would i use it with received argument as this string "/Date(1633421520000)/"
not sure how to create my date object from this string so ill be able to use date-fns functions.
You need to extract the number (which looks like milliseconds since unix epoch) from the string:
"/Date(1634717139973)/".match(/\d+/)[0]
Then use Date constructor like so:
var date = new Date(Number("/Date(1634717139973)/".match(/\d+/)[0]));
date.toISOString(); // 2021-10-20T08:05:39.973Z
Related
This question already has an answer here:
.diff is not a function on moments.js
(1 answer)
Closed 4 months ago.
I have a date "22-Mar" (YY-MMM) and I want to format it in "01.03.2022" and after that transform it into a date object without using "new Date()" method.
I heard that I can do this with MomentJS but I think I write something wrong.
I tried to format this date like that but this doesn't work.
moment(date, 'YY-MMM').format('MM/01/YYYY').toDate()
My error message:
TypeError: moment(...).format(...).toDate is not a function
How should I resolve the problem?
format() returns a string, where as the toDate() is only available on the MomentJS object.
So remove the format() if you want to convert it to a Date
If you use the 'format' to set the day to 1, you can use date() for that: .date(1)
Also, your custom format was invalid, I've changed it to DD-MMM to the date is proper parsed
const input = '22-Mar';
const mom = moment(input, 'DD-MMM').toDate();
console.log(mom);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
This question already has answers here:
Moment.js - How to convert date string into date?
(3 answers)
Closed 3 years ago.
I am getting my date from PHP in format ymd. I am using Moment.js to convert it to format I need, but it's not working properly.
So for example today's date is comes like 190528 but after conversion to YYYY-MM-DD it becomes 190528-1-1 instead of 2019-05-28.
Is it the first format of the date which causes the problem, or there is a way to overcome this and convert it the way I need?
Parse Date first by passing format as 'YYMMDD'
moment("190528", "YYMMDD").format("YYYY-MM-DD");
This will give proper output.
You do not need to load moment for this.
You can break the string into parts of years, month and date. Format the year and return the joined string...
function formatDate(date) {
var _d = date.match(/.{1,2}/g);
_d[0] = "20" + _d[0];
return _d.join("-");
}
var d = "190528";
console.log(formatDate(d));
You need to tell moment what format your date string is in:
console.log(moment("190528", "YYMMDD"))
<script src="https://unpkg.com/moment#2.24.0/moment.js"></script>
You can pass the format of current date as a second param of moment.
console.log(moment("190528", "YYMMDD").format("YYYY-MM-DD"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Here the docs.
This question already has answers here:
Converting Unix timestamp to Date Time String [duplicate]
(2 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I want to know how to convert a long date like this 1542814586896 into a String format like this 2019/02/05
You can use Date class for setting time in integer format and getting any values like day, month, year
let date = new Date(1542814586896);
console.log(date.getDay(), date.getMonth(), date.getFullYear())
You can use
new Date(1542814586896).toLocaleDateString(`ja-JP`);
//-> "2018/11/21"
.toLocaleDateString() formats time into a format of a specific region. In the example above, time's formatted into Japanese format (just because it seems like in Japan they use exactly the format you need).
What's cool about this method is that you may just pass no argument to toLocaleDateString & it will then just automatically pick the format that the final user prefers (or more precisely, the format that is set in user's OS).
For example in my browser:
new Date(1542814586896).toLocaleDateString();
//-> "21/11/2018"
However, if I had Egyptian Arabic set as main language of my operating system, the result should be like:
new Date(1542814586896).toLocaleDateString();
//-> "٢١/١١/٢٠١٨"
You may find more information about different locales & corresponding formats here.
This question already has answers here:
Date conversion .NET JSON to ISO
(4 answers)
Convert Json date to "Date" in javascript
(3 answers)
Closed 5 years ago.
I have a JSON Object that when stringified looks like this [{"x":"/Date(1451606400000)/","y":877282.57}]
and I want to get the numeric part of the date 1451606400000
I can use regex, but is there an easier way involving parsing the date object? Perhaps I can construct a date from that value and then call a method to get the numeric component?
The question is quite confusing.
When you say "I want to get the numeric part" I don't get it.. maybe you can be more explicit.
My anwser assummes you want to convert a string value to date object (again I might be wrong)
var dateString = "1451606400000"; //save string value to a varable
var dateInt = Number(dateString); //convert to number
//after converting to number parse the value into the date object.
//Note: parsing a string value direct will through an exception
var dateObject = new Date(dateInt);
console.log(dateObject); //Date 2016-01-01T00:00:00.000Z
This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 6 years ago.
Javascript code:
var date = new Date(1466278504960);
return: Sat Jun 18 2016 20:35:04 GMT+0100 (WEST)
How can I convert the same number to date in python ?
When I use
datetime.datetime.fromtimestamp(int("1466278504960")).strftime('%Y-%m-%d %H:%M:%S'))
I receive this error: ValueError: year is out of range
datetime.datetime.fromtimestamp will do this, but you need to divide the value by 1000 first (the numeric value you give and JavaScript's Date expects is in milliseconds since the epoch, where Python's API takes a floating point seconds since the epoch):
from datetime import datetime
date = datetime.fromtimestamp(1466278504960 / 1000.)
That makes the raw datetime object; if you want it formatted the same, you should take a look at datetime object's strftime method.
It's almost the same. You just have to convert the units.
Date from javascript specifies the number in milliseconds, in other words, expects a number in millisenconds as a parameter. When the python date takes seconds.