In react app, I am getting an timestamp like 1635424532797 and I am converting and trying to display a date using new Date(1635424532797).toLocaleDateString("en-US").
Right now I am getting Invalid Date for this.
I am getting createdAt timestamp from backend data and parsing it to Date function.
How can I display a date (or datetime) here? Please help me to solve this issue.
This is an example how to do Date in javascript, sample does new Date object with string and formats to en-GB
const date = new Date(YOURDATE)
date.setDate(date.getDate());
const formattedDate = date.toLocaleDateString('en-GB', {
day: 'numeric', month: 'short', year: 'numeric'
}).replace(/ /g, ' ');
Related
We use formatGMT() or format() methods of date time object in apex, it returns the formatted date based on the time zone. Although what should be done if we have a static date, such as date of birth that needs to be formatted, irrespective of the local time zone.
In my scenario when using DateTime.format(), The date of birth is showing correctly for users in India, although for the users in US, yesterday's date is displayed in some cases. In some cases it's converted correctly.
For eg. The dob for x person is 1995-09-20 which gets formatted to 09/20/1995,
Whereas for y person, the same method generates a formatted date 05/15/1990 when dob is 1990-05-16.
Basically the formatted date should be the exact replica of the dob, irrespective of time zone.
Method:
private static String localeFormatDate (Date dateValue) {
try{
String format = String.isNotBlank (strDateFormat) ? strDateFormat: 'MM/dd/YYYY';
Date myDate = Date.newInstance (dateValue.year(), dateValue.month(), dateValue.day());
Time myTime = Time.newInstance(0, 0, 0, 0);
return DateTime.newInstanceGMT (myDate, myTime). format GMT (format);
} catch (Exception ex){
return null;
}
}
Additionally, I have also tried to set timezone as Asia/Colombo as the date is correctly formatted in that time zone:
return DateTime.newInstanceGMT(myDate, myTime).format (format, 'Asia/Colombo');
Although I'm not sure if that would generate the correct format date based on the when the method runs.
Goal:
I need to first create a String representing the current date.
Afterwards this String needs to be parsed and used to build an instance of the Date class.
Initial attempt:
In my test class I create a current date as a String input for my tested method in the following manner:
String inputDate = date.today().format(); // 13:28:15:378 USER_DEBUG [24]|DEBUG|17.3.2017
However, when I attempt to create an instance of a Date object like this:
Date dateFromInput = date.valueOf(inputDate);
I receive the following error:
13:28:15:398 FATAL_ERROR System.TypeException: Invalid date: 17.3.2017
Following code
((DateTime)Dob).format('YYYY-MM-dd')
Just Works
Date.format() will return string in current local date format of logged in user.
Date.valueOf needs input string in format yyyy-MM-dd HH:mm:ss in the local time zone.
Below should work:
String inputDate = date.today().format('**yyyy-MM-dd HH:mm:ss**');
Date dateFromInput = date.parse(inputDate);
In the documentation, there is a difference between the parse and valueOf Date methods that escaped me:
parse(stringDate)
Constructs a Date from a String. The format of the String depends on the local date format.
valueOf(stringDate)
Returns a Date that contains the value of the specified String.
What I wanted was the parse:
String inputDate = date.today().format(); /
Date dateFromInput = date.parse(inputDate);
You can try Moment.apex. Here is the link
Datetime dt = new Moment('2018/01/12 10:00:00', 'yyyy/MM/dd HH:mm:ss').toDatetime();
I have date in this format
2016-05-22 08:00:00
I am trying to apply a filter like this
<td>{{ event._source.event_date | date : "dd.MM.y"}}</td>
but this does notthing.
What am I missing?
The date pipe requires the value of type Date. A value of string is currently not supported (but work-in-progress).
You can convert the date using new Date('2016-05-22 08:0:00') (not checked if the format is correct and accepted though)
Angular 1:
Add following function to your scope to get Date object from your string. First it converts your string with date to format YYYY-MM-DDTHH:MM:SS (adding 'T' between YYYY-MM-DD and HH:MM:SS).
$scope.isoDate = function(dateString) {
return new Date(dateString.split(' ').join('T'));
};
Now you can use it:
{{ isoDate(event._source.event_date) | date : "dd.MM.y" }}
See jsfiddle
I using Django-rest and I want to get month form Datefield in AngularJS.
My code is date = '2014-2-30' and I get month: var x = (new Date (Date.parse (date).getMonth().
But x = 1 and value of year = 114.
I read https://docs.angularjs.org/api/ng/filter/date but I can't do it. Would you give me example?
Django REST Framework should pass dates back to Angular in ISO 8601 format (yyyy-MM-dd), so there shouldn't be an issue parsing it on Angular's side.
{{ obj.date | date:"MMMM" }}
MMMM is the Angular date code for the full month (January-December). You can find other Angular date codes in the date filter documentation.
I am developing Windows Form using vs2010 C#
I have 2 datetime pickers
1 is fromDatePicker and the other is ToDatePicker
I want to validate that the toDate is always after from date are the same day
eg if From:30/8/2010...To:16/8/2010
An error message is showed to user.
You can do comparisons on `DateTimes'
if (toDate < fromDate)
{
MessageBox.Show("To date is before from date");
}
If you're not worried about the time portion then use the Date property:
if (toDate.Date < fromDate.Date)
{
MessageBox.Show("To date is before from date");
}
Can you set a minimumDate property on your toDate control to the date on the fromDate control?