convert date string into the actual date format? - javascript

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');

Related

How to convert RFC 3339 datetime into regular JavaScript format

I need to use the Google Calendar service for editing single instances of an event series. The service returns datetime values in RFC 3339 format (like 2022-11-03T21:30:41.043Z), which is hard to process within Google apps script. Is there an easy way feeding it into Utilities.formatDate to convert it (ie yyyy-MM-dd HH:mm)?
Vice versa is plainly done with .toISOString().
You can convert an ISO8601 format date string into a JavaScript Date object directly with the Date constructor. To convert the Date object into a formatted date string, use Utilities.formatDate() with your preferred timezone, like this:
function test() {
const dateISO8601 = '2022-11-03T21:30:41.043Z';
const timezone = 'PST';
const dateString = toDateString_(dateISO8601, timezone);
console.log(dateString);
}
function toDateString_(dateISO8601, timezone = 'GMT') {
const date = new Date(dateISO8601);
return Utilities.formatDate(date, timezone, 'yyyy-MM-dd HH:mm');
}

How to convert to date-fns?

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)))

How to convert into Date time

When the date is passed from my c# to JavaScript it returns the date time as {4/3/2020 12:00:00 AM}
but in JavaScript it is shown as 1585852200000.
What is the format that is being used? And how can i convert it back?
You need to convert the Unix timestamp to DateTime format,
var localDate = new Date(1585852200000).toLocaleDateString("en-US")
console.log(localDate); // only local date
var localTime = new Date(1585852200000).toLocaleTimeString("en-US")
console.log(localTime) // only local time
// local datetime
console.log(new Date(1585852200000).toLocaleString());
1585852200000 is epoch date.
you can convert it as
var date = new Date(1585852200000)
console.log(new Date(1585852200000));
As an alternative from Shivaji's answer:
When you are passing the date through to JS you could cast it as a string with DateTime.ToString("dd/MM/yyyy") seen here on MSDN.
This will keep its integrity visually, if it is just for display purposes, otherwise you will need to re-cast appropriately in JS (in which case use Shivaji's answer).
JavaScript Date's object will return the DATE object and it's POSITION that is being assigned in your computer. So, when you are working with a date or datetime types, you can use some of the methods that are provided by the Date object, such as getDate() and getDay(). But, a better solution would be to format the Date object itself. For example: use the toString() or toUTCString() methods.
var d = new Date();
document.getElementById("demo").innerHTML = d.toString();
Reference:
https://www.w3schools.com/js/js_date_formats.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

How can I reverse date time to ajax date time

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.

change date format javascript

I need to send a date to a backend service that requires a date in the following format.
I have access to moment also.
I am using an input type of datetime on the front end which sends over a date like this: "2017-05-17T10:00"
I have tried new Date("2017-05-17T10:00"); but this returns Wed May 17 2017 11:00:00 GMT+0100 (BST). I have also tried using some moment methods, but cannot get the correct format.
Does anyone know how I can convert the datetime string - "2017-05-17T11:43" to the following '2017-05-17T10:43:03+0100'?
Try moment.format(). Here is the list for reference https://momentjs.com/docs/#/displaying/format/
var dt = new Date("2017-05-17T10:00");
console.log(dt);
//'2017-05-17T10:43:03+0100'
var z = moment(dt).format("YYYY-MM-DDTHH:mm:ssZZ");
console.log(z);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Your date is in ISO 8601 format. If you have access to Moment.js (as you said) you can use format() method as below:
var date = moment("2017-05-17T10:00").format("YYYY-MM-DDTHH:mmZZ");
console.log(date);
// prints "2017-05-17T10:00-0300"
Try it.

Categories