This question already has answers here:
Convert UNIX timestamp to date time (javascript)
(4 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
Im trying to make a table with vue.js and one of the row is supposed to print a date i receive in epoch time. but fail to convert it in a readable date and just get "invalide date" printed on my browser.
Here is how i convert it
var sec = 1588351494;
var tmp =new Date(0);
tmp.setUTCDate(sec);
var res= tmp;
her is how it's made to be able to be called as a vue.js object
return {
tableData: [
{
uid: '01020304050607',
lastReadDate: res,
},
]
}
And then i simply print it in my html page doing this, which print the "invalide date" in the row.
<td>
{{row.lastReadDate }}
</td>
var sec = 1588351494;
var res =new Date(sec * 1000); // multiply seconds with 1000 to convert it to ms.
Check the fiddle: https://jsfiddle.net/vf74h5n0/4/
To format date in "DD/MM/YYYY" you can use momentjs.
var date = new Date(1588351494 * 1000);
moment(date).format("DD/MM/YYYY");
Here is a example https://jsfiddle.net/vf74h5n0/5/
Related
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 10 months ago.
I'm trying to create a date with the format 'mmddyyyy'. I've noticed I can pass a four digit string, but when I use the format above, it says invalid date. How can I go about this with pure js?
let dateString = '01012022'
let d = new Date(dateString)
maybe you want something like that
let dateString = '01012022'
let [match, dd, mm, yyyy] = dateString.match(/(\d{2})(\d{2})(\d{4})/)
// iso format
let d = new Date(`${yyyy}${mm}${dd}`)
Seems like you have to split your string into pieces then use common Date() constructor with separated day, month and year
//Your string with date
let dateString = '01012022';
//extracting 2 first numbers as m (month), 2 second as d (day) and the last 4 as y (year)
let {d,m,y} = /(?<m>\d{2})(?<d>\d{2})(?<y>\d{4})/.exec(dateString).groups;
//then create new date using constructor
let date = new Date(y,m,d);
This question already has answers here:
setDate() returns number 1603240915215 instead of a date
(2 answers)
Closed 2 years ago.
I a trying to save a date to the nextMonth. For that I am first setting the month to next 30 days. But the final output date it is giving me in milliseconds.
I want the date in GMT format strictly.
What can I do for that?
var snm = new Date();
snm = snm.setDate(snm.getDate() + 30);
console.log("snm = "+ snm);
Try this
var snm = new Date();
snm.setDate(snm.getDate() + 30)
console.log("snm = "+ snm.toString());
This question already has answers here:
Calculating the difference between two dates
(10 answers)
Check if a date is 24 hours old
(5 answers)
What's the best way to calculate date difference in Javascript
(2 answers)
Closed 2 years ago.
I need to fetch bitbucket repositories and filter them according to the updated_on timestamp.
GOAL: Is to filter the ones which are updated in last 24 hours.
This is what I get from API : updated_on: 2018-01-30T11:45:32.902996+00:00
My code logic:
// repo's updated_on timestamp (example)
const time = '2018-01-30T11:45:32.902996+00:00'
let currentTime = Date.now()
let updatedOn = new Date(time).getTime()
const filter = () => {
// boolean
return (currentTime - updatedOn) > 86400000)
}
if (filter()) {
// NOT FILTERING // giving out last month's data as well
console.log(updatedOn)
}
Ciao, with moment library you could use isBefore function in this way:
let date = "2018-01-30T11:45:32.902996+00:00";
let anotherDate = moment(); // today
console.log(moment().subtract(24, 'hours').isBefore(moment(new Date(date))));
console.log(moment().subtract(24, 'hours').isBefore(moment(new Date(anotherDate))));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
This question already has answers here:
Parse DateTime string in JavaScript
(9 answers)
Closed 5 years ago.
I have problem with convert date which I get from API. Format is for example "16/09/25"
I try do it like this
var x = new Date(dateFromApi)
and console thorw me error.
Parsing a date string is very simple. A function that will work in any host from IE 4 onward is:
function parseDMY(s) {
var b = s.split(/\D/);
return new Date(b[2], b[1]-1, b[0]);
}
console.log(parseDMY('16/09/25'));
Where the year is >= 0 or <= 99, 1900 is added so 25 becomes 1925. Preserving years in this range (so 25 is 0025) requires an additional line of code.
It is safest to provide the Date constructor with the individual parts of the date (i.e., year, month and day of the month).
In ES6 you can provide those elements like this:
var x = new Date(...dateFromApi.split('/').reverse().map( (p,i) => p-(i%2) ));
The map is needed to subtract one from the month number, as it should be zero-based in numeric format.
Note the new Date(year, month, day) version of the constructor will assume 19xx when you provide only 2 digits.
var dateFromApi = "16/09/25"
var x = new Date(...dateFromApi.split('/').reverse().map( (p,i) => p-(i%2) ));
console.log(x.toDateString());
In ES5, it would be a bit longer, like this:
new (Date.bind.apply(Date, (dateFromApi+'/').split('/').reverse()
.map(function (p,i) { return p-(i==2); })));
var dateFromApi = "16/09/25"
var x = new (Date.bind.apply(Date, (dateFromApi+'/').split('/').reverse()
.map(function (p,i) { return p-(i==2); })));
console.log(x.toDateString());
Of course, this assumes that the input format is consistently in the order DD/MM/YY (or D/MM/YYYY, as long as the order is the same); that valid dates are passed, and that you accept how 2-digit years are mapped to 4-digit years.
Your format is DD/MM/YY and it is not accepted by Date and will throw an error.
This is because, as mentioned by #MattJohnson, the accepted Date formats vary by locale and the only official format is YYYY-MM-DD (which is derived from ISO date string. Read here).
In most cases, Date will accept the format YY-MM-DD. So we can simply do this:
var date = "16/09/25"; // date received from API
var split_date = date.split('/'); // outputs ["16","09",""25"]
var rearranged_date = [split_date[1], split_date[0], split_date[2]].join('/'); // outputs "09/16/25"
var proper_date = new Date(rearranged_date);
In other cases, it is best to provide the full-year YYYY instead of just YY.
This question already has answers here:
JavaScript date objects UK dates
(6 answers)
Closed 9 years ago.
I have the below code. If we try to retrieve the day, month and year in UK server setup. The date object returns an incorrect value.
var startDate = "30/08/2013";
var d = new Date(startDate);
alert(d.getFullYear()); //2015
Please help me
Use moment.js if you want better control of parsing:
var startDate = "30/08/2013";
var m = moment(startDate,"DD/MM/YYYY");
alert(m.year()); //2013
Try passing the date as a list of arguments instead:
var startDate = "30/08/2013"; // Would also work with 30-08-2013
var startDateArray;
if(startDate.contains('/'))
startDateArray = startDate.split('/');
else if (startDate.contains('-'))
startDateArray = startDate.split('-');
else if (startDate.contains('.'))
startDateArray = startDate.split('.');
var d = new Date(startDateArray[2], startDateArray[1]-1, startDateArray[0]); // Month is 0 based, that's why we are subtracting 1