moment.js convert to ISO output null + warning on specific date - reactjs

I am using "moment": "2.22.2" in my React application
and I have two strings dates, when i convert them using
moment().toISOString() one of them are return null.
null is return for all dates newer than 13.11.2019
for exam: 14.11.2019 & 15.11.2019 &16.11.2019 ...
const date1 = '12.11.2019 23:55';
const date2 = '13.11.2019 23:55';
moment(date1).toISOString() // => 2019-12-11T22:55:00.000Z
moment(date2).toISOString() // => null
is it something wrong locally in my application ? or it is an error because of moment library ?

I think you should parse your strings using a custom format since without it moment thinks 12.11 is the 11 of december (in the form [month].[day]).
use this form instead:
const date1 = '12.11.2019 23:55';
const date2 = '13.11.2019 23:55';
moment(date1,'DD.MM.YYYY HH:mm').toISOString()
moment(date2,'DD.MM.YYYY HH:mm').toISOString()
See docs here (https://momentjs.com/docs/#/parsing/string-format/)

Related

Converting month number to month name

I'm in struggle trying to convert a month number that i'm getting from API to month name and then render the month name in Datatable.
Example:
month (1) => "January"
month (2) => "February"
...etc
For that, i created this function:
function getMonthName(monthNumber) {
const date = new Date()
date.setMonth(monthNumber - 1)
return date.toLocaleString('en-US', { month: 'long' })
}
It works fine if i provide monthNumber correctly.
Now i would like to use this function inside month column (so it will show month names instead of month numbers).
What i'm expecting is: getting different month names at each row successively.
What i'm getting is: table always show January only on all its rows.
I reproduced my issue here:
Sandbox: https://codesandbox.io/s/hopeful-goldberg-xhqvjm-forked-wwq3m0?file=/src/components/tables/SalesTable/SalesTable.jsx
You can use the js-date helper library. I am using date-fns.
Supported formats
npm install date-fns --save
import {format} from 'date-fns'
// Let say its January
const month = 1
// date can be anything
const date = `1970/${month}/07`
const monthName = format(new Date(date), 'LLLL')

Date/Time Formatting in React

I am working with an API that returns Dates in the format 2022-03-01T11:32:37
Created: {this.props.proposal.OPENWHEN}
Created: 2022-03-01T11:32:37
How do i format this into DD/MM/YYY 24:00:00 ?
Thanks In Advance
Something like the following should be enough:
// Example expected argument: 2022-03-01T11:32:37
function prettifyDateTime (str) {
// Splitting the string between date and time
const [date, time] = str.split("T");
// Assuming 03 is the month and 01 is the day – otherwise, those could be swapped
const [year, month, day] = date.split("-")
// Added slashes and the space before the time
return `${day}/${month}/${year} ${time}`
}
prettifyDateTime("2022-03-01T11:32:37") // '01/03/2022 11:32:37'
Otherwise, I recommend using a date library like date-fns, dayJS, or moment.
You can use the Date object to accomplish what you want. It'll also accept other date formats. Here is one place to find Date description. You also get the benefit of rejecting invalid date formats
function prettifyDateTime(str) {
let date = new Date(str);
// There's other ways of formatting this return
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
}

Display rows with current date as top result using react-table

I'm trying to create a table using react-table that would display data that matches today's date as the top result. For instance, today is 18 August 2021, so I would like any data entry that matches 08/18/2021 (mm/dd/yyyy format) to be displayed as the topmost entry in my table.
My current table looks like this
To give an example, I have already sorted my table in chronological order (from earliest to latest in mm/dd/yyyy format). However, I want any entry/entries in the table above with today's date (eg. Date of Operation: 08/18/2021) to be displayed at the top of the table. Thereafter, entries that do not match today's date will continue to be sorted in chronological order.
Can someone please let me know how this can be done? Thank you.
Without getting into the code details, you should be able to do this by supplying a custom sort function in the column definition API docs. We don't do this specific sort but we do have multiple custom sort functions, and I got the most help in writing them by reading the sortTypes.js file from the library source.
Here's a simple date sort function. You basically get two rows and a columnId and return -1, 0, or 1.
const prevDate = new Date(prev.original[columnId])
const prevIsDate = ...
const currDate = new Date(curr.original[columnId])
const currIsDate = ...
if (prevIsDate && currIsDate) {
return prevDate > currDate ? 1 : -1
} else {
if (!prevIsDate) {
return !currIsDate ? 0 : 1
} else {
return 1
}
}
}

How to check created_at exists or not between two date

I have two dates. I want to check that created_at is existed or not between two dates that I will provide.
$form_date = 08/07/2018 // ('m/d/Y')
$to_date = 08/014/2018 // ('m/d/Y')
I have tired
Model::whereBetween('created_at', [$form_date, $to_date])->get();
But it returns empty.
you can solve this by do the following:
$form_date = Carbon::createFromFormat('d/m/Y',$request['form_date'])->format('d-m-Y');
$to_date = Carbon::createFromFormat('d/m/Y',$request['to_date'])->format('d-m-Y');
Model::whereBetween('created_at', [$form_date, $to_date])->get();
Try putting your boundary dates into an ISO format which MySQL can directly use:
$from_date = '2018-08-07';
$to_date = '2018-08-14';
Model::whereBetween('created_at',
[new Carbon($from_date), new Carbon($to_date)])->get();

Epoch 0 to Date conversion using momnet.js

I need to convert my epoch date to a Date object. I did this using the following code.
let abc = moment(maintenance.actualEndDate * 1000).format('DD/MMM/YYYY hh:mm');
but there is a high chance to 0 as the value for 'maintenance.actualEndDate'.
in this case the transalated date is showing the value as '01/01/1970 12:00'.
I actually need as an empty string in variable abc if the maintenance.actualEndDate is 0
I'm working on angular 4, is there any optimal solution to this?
If what you need is converting non-zero timestamps to a formatted date, and zero timestamps as empty string, a dedicated function would be nice:
function formatTimestamp(secondsSinceEpoch) {
return secondsSinceEpoch===0 ? '' : moment.unix(secondsSinceEpoch).format('DD/MMM/YYYY HH:mm');
}
//...
let abc = formatTimestamp(maintenance.actualEndDate);
(But this has nothing specific to angular)

Resources