I tried different method to develop date picker in reactJS. I done with "react-datepicker" package and it return "Wed May 15 2019 12:54:33 GMT+0100" as a result but I need 12/12/2000 format
try to format it as you want
const pickerDate = new Date('Wed May 15 2019 12:54:33 GMT+0100')
const day = pickerDate.getDay() < 10 ? '0' + pickerDate.getDay() : pickerDate.getDay()
const month = pickerDate.getMonth() + 1 < 10 ? '0' + (pickerDate.getMonth() + 1) : pickerDate.getMonth() + 1
const year = pickerDate.getFullYear()
const formatedDate = `${day}/${month}/${year}`
you can also use third part libraries like moment
Related
Is there any way to convert normal string 5pm into 2022-04-20T17:00:00.000Z format?
I have got this from backend but Im using timepicker in antd. It only accepts 2022-04-20T17:00:00.000Z format and it is in string format in my DB.
This should consistently give you the current date with the time attached
function convertToISO(timeString) {
const [hour12, ampm] = timeString.split(/(?=[ap]m$)/i)
const hour = hour12 % 12 + (ampm.toLowerCase() === 'pm' ? 12 : 0)
const date = new Date()
// Set time, adjusted for time zone
date.setHours(hour, -date.getTimezoneOffset(), 0, 0)
return date.toISOString()
}
console.log(convertToISO('5pm'))
There is start date and end date. Using moment ,will get difference between 2 dates in hours.
var now = moment(sessionData.StartTime);
var end = moment(sessionData.EndTime);
var duration = moment.duration(end.diff(now));
var days = duration.asHours();
it returns : 3.08 .
I want to show that difference like this- 3 hrs 15 m.
Is this possible to achieve
Try get the difference between two days using diff followed by expressing it terms of duration. Then you would be able to get exact number of days, hours, minutes
let now = moment("2017-01-26T14:21:22+0000");
let expiration = moment("2017-01-29T17:24:22+0000");
let diff = expiration.diff(now);
let diffDuration = moment.duration(diff);
let dayDiff = diffDuration.days() + "d";
let hoursDiff = diffDuration.hours() + "hrs";
let minDiff = diffDuration.minutes() + "m";
console.log(`${dayDiff} ${hoursDiff} ${minDiff}`);
I am fetching time from the database to my app.
I want to fetch time from this 2018-10-17 14:52:52 format to this 2:52 PM, 17 Oct format.
How can I format the time without using the 'moment' library?
const input = new Date('2018-10-17 14:52:52');
const output = input.toLocaleTimeString('en-US', {timeStyle: 'short'}) + ', ' + input.toLocaleDateString('en-US', {day: 'numeric'}) + ' ' + input.toLocaleDateString('en-US', {month: 'short'});
console.log(output); // 2:52 PM, 17 Oct
Actually in my app. I am trying to display current date & time in a specific format like this " 27/02/2019 1:40 PM ". I have done this by making use of custom formatting codes. But what i actually need is, I need to achieve this by making use of libraries.
Thanks for helping.!
Using this:(Manually formatting Date & Time)
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var hours = d.getHours();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
var min = d.getMinutes();
min = min < 10 ? '0'+min : min;
var result = date + '/' + month + '/' + year + ' ' + hours + ':' + min + ' ' + ampm;
console.log(result); //Prints DateTime in the above specified format
Use moment.js https://momentjs.com/
moment().format('DD/MM/YY h:mm A') will give you your desired output.
I am fetching the date from my Django backend which comes like this: 2016-03-31
In my angular controller I want to compare it with today's date and if they are similar I want to deactivate a button.
I tried new Date() which gives me something like Thu Mar 31 2016 08:59:01 GMT+0200 (W. Europe Daylight Time)
How can these two dates be compared to achieve my goal?
ehhhhh... i think currently we could only do a manual formatting
see this one: How to format a JavaScript date
For your reference, below is what i did though:
$scope.formatDate = function(date){
var newDate = new Date(date);
var year = newDate.getFullYear();
var month = (newDate.getMonth() + 1).toString(); //add 1 as Jan is '0'
var day = newDate.getDate().toString();
month = month.length > 1? month: '0'+month;
day = day.length > 1? day: '0'+day;
$scope.date = day + '/' + month + '/' + year;
}
If you want to compare the date with now you can do the following:
var now = new Date().getTime();
var compareDate = new Date('2016-03-31').getTime();
if(now > compareDate){
//greater
}else if(now < compareDate){
//less
}else{
//equal
}
Just add what you need to the scope and then you could do something like:
ng-disabled="compareDate === today"