I have following moment expression:
<DialogContent>
{startDate ? (
<DateTimePicker
value={startDate}
onChange={(value: any) =>
setStartDate(moment(value).format())
I would like to convert to date-fns format, but it fails, it is always 1970 something, why? I converted like this:
setStartDate(getUnixTime(new Date(value)))
You can import format function from date-fns and use it:
import { format } from 'date-fns';
// Pass desired format as a second parameter
setStartDate(format(value, 'yyyy-MM-dd'));
moment(value).format() return a ISO 8601 date string, look like: 2021-11-10T10:19:09+09:00. Then, I guess the setStartDate function require a date string with ISO 8601 format.
The goal is using date-fns to create a string as the result of moment.format.
To do that, you need to create a date object. If it works with moment(value) syntax, mean value is a millisecond timestamp number (1636507295498) or a formal date string (2021-11-10). Then you can create a date object by Date constructor:
new Date(value);
Now, you can use formatISO to create the required date string:
formatISO(dateObject);
The final look will be like this:
setStartDate(formatISO(new Date(value)))
Related
I have been trying to format a zulu format date to normal date i.e. MM/dd/yyyy, so far I have tried date-fns and moment. The date I have is 2022-11-03T19:48:24Z I am expecting it to be formatted as 11/03/2022 but I am getting 11/04/2022, I do not understand why it is adding a day to the date and how can I fix this ?
export function fDate(
date: Date | string | number,
template: string = 'MM/dd/yyyy'
) {
return format(new Date(date), template);
}
fDate('2022-11-03T19:48:24Z')
expected outcome 11/03/2022 but getting 11/04/2022
format is coming from date-fns
Do not use the Date constructor to parse a date_time string when you are using a library like moment.js or date-fns. Instead, use the applicable constructor or methods in the library you want to use.
moment.js
The symbols posted in your question do not match the required symbols. The symbols, d and D have different meanings. The same is the case with y and Y. Check the documentation page at https://momentjs.com/docs/
The following code produces the correct result as 11/03/2022.
var moment = require('moment');
console.log(moment("2022-11-03T19:48:24Z").format('MM/DD/YYYY'));
In your code, this will be implemented as
return moment(date).format('MM/DD/YYYY');
date-fns
The symbols posted in your question match the required symbols.
The following code produces the correct result as 11/03/2022.
const fns = require('date-fns')
console.log(fns.format(fns.parseISO('2022-11-03T19:48:24Z'), 'MM/dd/yyyy'))
In your code, this will be implemented as
return fns.format(fns.parseISO(date), 'MM/dd/yyyy');
Using moment's parseZone helped me
export function fDateISOStringToDate(
date: string,
template: string = "MM/DD/YYYY"
) {
return moment.parseZone(date).format(template);
}
I have called an API and then get a date which is string format like '15/07/21-23:59:59'. But I want to convert this string into the actual date format like this:
**15/07/21** OR **2009-06-01T10:00:00.000**.
so how can I achieve this?
It's strange to see a response returning a formatted date expression. By the way, your task would be easily done with momentjs. Here is my snippet:
// since your date format is not a standard one, you would have to pass an
// instruction of your date format as a second parameter to the moment constructor
const momentDate = moment('15/07/21-23:59:59', 'DD/MM/YY-HH:mm:ss');
momentDate.format('DD/MM/YYYY'); // => "15/07/2021"
momentDate.format(`yyyy-MM-dd'T'HH:mm:ss.SSSZ`); // => "2021-07-Th'T'23:59:59.000+07:00"
you can pass this string to a Date object as follow:
var date = new Date(YOUR STRING);
or you can use Date.parse() method if it does not work:
Date.parse('04 Dec 1995 00:12:00 GMT');
Using Luxon JS, I've been trying to format datetime to output in a certain format, using the native toISO function:
This is what I get:
"2018-08-25T09:00:40.000-04:00"
And this is what I want:
"2018-08-25T13:00:40.000Z"
I know that they are both equivalent in terms of unix time and mean the same thing except in a different format, I just want to be able to out the second string rather than the first. I looked through the Luxon docs but was unable to find any arguments/options that would give me what I need.
As other already stated in the comments, you can use 2 approaches:
Convert Luxon DateTime to UTC using toUTC:
"Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime.
Use toISOString() method of JS Date.
You can use toJSDate() to get the Date object from a luxon DateTime:
Returns a JavaScript Date equivalent to this DateTime.
Examples:
const DateTime = luxon.DateTime;
const dt = DateTime.now();
console.log(dt.toISO())
console.log(dt.toUTC().toISO())
console.log(dt.toJSDate().toISOString())
console.log(new Date().toISOString())
<script src="https://cdn.jsdelivr.net/npm/luxon#1.26.0/build/global/luxon.js"></script>
From documentation I saw that in the method .fromISO of DateTime you can add an option object after the string of ISO date ("2018-08-25T09:00:40.000-04:00" in your example). In this object specify zone: utc like that:
const DateTime = luxon.DateTime;
const stringDate = "2018-08-25T09:00:40.000-04:00";
const dt = DateTime.fromISO(stringDate, {zone: 'utc'});
console.log('This is your date format', dt.toISO())
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script>
I have a date which is like this "19/05/2020". I am trying to convert it into "yyyy-MM-dd" format using pipe. So the date should be look like "2020-05-19".
this._datePipe.transform("19/05/2020", 'yyyy-MM-dd');
It is giving me an error saying Unable to convert "19/04/2020" into a date' for pipe 'DatePipe
Also tried converting the string to date but again invalid date message is getting display.
Is there any way so that we can convert this date that is "19/05/2020" to a valid one so that we can perform datepipe on it.
I guess you need to convert your date having a supported formatted string :
For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
This is valid for Date.parse() or new Date().
So you'll need to parse your string with another method.
This may helps you :
How to convert dd/mm/yyyy string into JavaScript Date object?
In javascript I have some datetime like this
Date: '2017-07-04'
I want to convert it to DateTime like ajax get result.
Expect result like this:
'/Date(1565089870830)/'
How can I make it possible?
You can use Date.parse(). This method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
var date = Date.parse('2017-07-04');
console.log(date);
The format you're trying to create is a string containing an Epoch timestamp. To create that in JS you can create a Date object from the input string and retrieve the getTime() property. Then it's just a matter of concatenating that value in to the format needed. Try this:
var date = new Date('2017-07-04');
var epoch = date.getTime();
var output = `/Date(${epoch})/`;
console.log(output);
Presumably you're working with an ASP.Net MVC site, given the date format you're trying to build. One thing to note here is that you don't need to use that format when sending DateTime values back to the server. You can send any string so long as it can be bound to a DateTime instance by the ModelBinder. As such I'd recommend using an ISO8601 format instead.