Convert javascript date to json date format - javascript

I'm working with an API which expects json date format.
I need to convert my javascript date
Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)
To json date format
/Date(1405699200)/

Is anything of the following ok?
console.log(new Date('Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)').toJSON());
console.log(new Date('Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)').valueOf());

/Date(1405699200)/
Here 1405699200 is your date in epoch format.
You can do this :
var myDate = new Date("July 1, 1978 02:30:00"); // Your timezone!
var myEpoch = myDate.getTime()/1000.0;
document.write(myEpoch);
To convert human readable date format to Epoch format : https://www.epochconverter.com/programming/#javascript

Related

What is JavaScript's default Date format?

What is the name of this format?
Wed Jul 06 2022 14:42:13 GMT-0400 (Eastern Daylight Time)
This format is the default format that is output "Independent of input format" from new Date().
source - https://www.w3schools.com/js/js_date_formats.asp w3schools explains the format but fails to mention what it's called before getting into other ISO format types.
How do you turn a date into that format using Moment.js?
Example: 2022-07-15T00:00:00.000Z or 2015-03-25
console.log(moment(new Date()).toISOString());
// 2022-07-06T19:08:36.670Z
console.log(moment(new Date()).toString());
// Wed Jul 06 2022 15:08:36 GMT-0400
console.log(new Date());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)
console.log(new Date().toString());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)
You can see that moment(new Date()).toISOString() and moment(new Date()).toString() DO NOT output that same format as the default JS format.
moment(new Date()).toString() is close, but is still missing the (Eastern Daylight Time) which is part of the default.
What is the name of this format?
It's not ISO, it's a format specified in ECMA-262, read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString
To convert it to ISO use date.toISOString()
Btw. don't use w3schools.com this website has major errors
How do you turn a date into that format using Moment.js?
ISO: moment(new Date()).toISOString()
2015-03-25: moment(new Date()).format('yyyy-MM-dd')
ECMA-262: moment(new Date()).toDate().toString()
P.S. I would consider using a different library as moment.js isn't getting updates anymore, but if you don't have a choice it's fine.
Answer:
console.log(moment(new Date()).toDate());
That was way harder to find than it should have been.
console.log(moment(new Date()).toDate());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)
console.log(new Date());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)
console.log(new Date().toString());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)
Bonus:
console.log(moment(1234, moment.ISO_8601, true).isValid());
// true
console.log(1234 instanceof Date)
// false
console.log(new Date() instanceof Date);
// true

New Date is returning an Invalid Date

I'm trying to convert the date string to Date format.
It works fine, but the result is not deterministic.
console.log(new Date("2020-10-22T00:00:00Z"));
// Thu Oct 22 2020 02:00:00 GMT+0200 (Central European Summer Time)
console.log(new Date("2O2O-11-02T00:00:00Z"));
// Invalid Date
Why does it happen?
You used O when you should have used 0 (in the second one). I'm not sure how this might have happened...

Convert the date "Tue Aug 18 2020 12:30:44 GMT+0530" to "Tue Aug 18 2020 12:30:44 GMT+05:30"

I am getting the current date and time using new Date() and added as a text content for div tag.
Working snippet:
const currentDateTime = new Date();
document.getElementById('demo').textContent = currentDateTime;
<div id="demo">
</div>
It is working fine, but I am in the need to change the format of the date.
From:
Tue Aug 18 2020 12:30:44 GMT+0530 (India Standard Time)
To:
Tue Aug 18 2020 12:30:44 GMT+05:30 (India Standard Time)
Colon (:) needs to be added for the gmt like
GMT+05:30
In real application I am using moment js and getting the current date and time using moment(). So moment js way of solution would also help me.
We can use the "Z" format token to indicate the UTC offset in a +-HH:mm format using moment.js. We can also include the GMT literal in the format string as [GMT]. It's documented here
I'm using moment.tz here to force the timezone to +5:30, but assuming its your local time, you'll just do moment() instead!
For example:
let m = moment.tz("Asia/Kolkata");
console.log(m.format("ddd MMM DD YYYY HH:mm:ss [GMT]Z"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data-10-year-range.js"></script>
Since the index of : is always the same in your case, you can use the following method:
const text = 'Tue Aug 18 2020 12:30:44 GMT+0530 (India Standard Time)'
let textArr = text.split('')
textArr.splice(31, 0, ":")
const newText = textArr.join('')

Convert a UTC ISO string to a UTC Date object

I have a string in ISO format 2020-03-11T21:00:00Z. I need help with converting this string to a Date object but keep it as UTC timezone. The expected output is Wed Mar 11 2020 21:00:00 GMT+0000 (UTC).
I've tried moment.utc('2020-03-11T21:00:00Z').toDate() but the output was in my local timezone: Thu Mar 12 2020 05:00:00 GMT+0800 (Malaysia Time).
Thanks

What is the name of this datetime format: "Sat Jul 28 00:00:00 MST 2012"

What is the name of this datetime format?
Sat Jul 28 00:00:00 MST 2012
P.S.I'm using JavaScript.
That is raw printing of JavaScript Date object, same as date.toString();
I think it is the RFC1123 date format.
http://www.hackcraft.net/web/datetime/#rfc822
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#RFC1123
https://www.rfc-editor.org/rfc/rfc1123
https://www.rfc-editor.org/rfc/rfc822

Categories

Resources