Formatting JSON date - javascript

I am converting json date from something like:
/Date(1224043200000)/
to
Mon Oct 22 16:37:04 UTC+0800 2012
using
var date = new Date(parseInt(dateData.substr(6), 10));
Is there any way to change the format to just show the month, date and year (Oct 22, 2012) instead of including the timezone and current day using similar if not the same code as the one I'm already using? Thanks so much.

Checkout datejs which has powerful formatting capabilities. Using the toString FormatSpecifiers, you can provide a custom pattern like this:
new Date().toString("MMM dd yyyy");

You can try this:
var formattedDate = date.toString().split(" ").slice(1, 4).join(" ");

You may want to use momentjs
With something like this :
moment(1224043200000).format("MMM Do, YYYY");

Related

How to convert date in javascript?

I try to use new Date() in javascript, and it display like this
Sat Dec 17 2016 00:00:00 GMT+0800 (your country standard time)
but I want to convert to display like this
2020-02-05T06:23:34
I try to search it in google but I don't know the term that Im gonna use to search the date.
new Date().toJSON().slice(0,19)
You can use the Following Methods:
let date = new Date().toISOString().slice(0,19)
or
let date = new Date().toJSON().slice(0,19)
You can find similar methods from here:
https://www.w3schools.com/jsref/jsref_toisostring.asp
You can use Date.toISOString
The timezone is always zero UTC offset, as denoted by the suffix "Z"
console.log(new Date(`Sat Dec 17 2016 00:00:00 GMT+0800 `).toISOString())
You can use moment.js library for that
var date=new Date();
let c=moment(date).format();
console.log(c)
let s=c.split("+")
console.log(s[0])
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
With vanilla JS you could use toISOString.
Go ahead and try
(new Date()).toISOString();.
Output would be in this format: "2019-02-01T05:43:11.618Z".
If you don't need the time zone you can format it, or use character handling.
Otherwise, use a library like moment.js or date-fns to handle date formatting.

Create new Date object by using moment.js

I have string:
date = "2019/1/16 00:00 +0900"
I'm in New York (timezone -5), I want to create an Date object like that:
Wed Jan 16 2019 00:00:00 GMT+0900
I can't use javascript to convert. It will return with timezone -5.
I use moment.js:
moment.tz(date, 'Asia/Tokyo').format('YYYY/MM/DD HH:MM');
However, it's not right. Could you please help me. Thank a lot.
It will work if your date object is a moment instance:
moment.tz(moment(date), 'Asia/Tokyo').format('YYYY/MM/DD HH:MM Z')
Timezones are difficult to get right. I think an easy-to-follow and somewhat idiomatic solution is this:
const date = "2019/1/16 00:00 +0900";
// parse in any timezone
const dateMoment = moment(date);
// deliberately set the timezone in which the moment is interpreted in
const timezonedMoment = dateMoment.tz('Asia/Tokyo');
// format the moment
const formattedDate = dateMoment.format('YYYY/MM/DD HH:MM z');
Of course, you would write this in a more concise form.

How can I set up a correct format while doing new Date() in jQuery?

I have a date in a following format:
12/11/2015 07:12 PM
In jQuery I'm doing:
var parsedDate2 = new Date(date);
alert(parsedDate2);
And that prints me:
Fri Dec 11 2015 07:12:00 GMT+0100 (Central European Standard Time)
and that almost works correctly, mostly because in my example (12/11/2015 07:12 PM) the format is DD/MM and not MM/DD. However, jQuery treats it as the month is first. That's a problem, because when I chose as input:
19/11/2015 07:17 PM <--- (19th of November)
I'm getting:
Invalid date
So how can I set up the correct format here with the day before the month?
Ugly, but it work, with JS only :
a = "12/11/2015 07:12 PM";
b = a.split(' ');
c = b[0].split('/');
bad = new Date(a);
alert('bad : '+bad);
good = new Date(c[1]+'/'+c[0]+'/'+c[2]+' '+b[1]+' '+b[2]);
alert('good : '+good);
The other way is to use Moment.js parsing tool
Think that you should use more specialized and focused library along with JQuery, for me the best one is Moment.js - it has all and more than needed to date-time parsing and formatting and doesn't do something else.
Also, there are some other alternatives, like date.js and globalize.js
It's in the form of mm/dd/yyyy. Try 11/19/2015 07:17 PM. Sadly, jQuery doesn't know which format you're using and so, uses the deafult one.
Unfortunately, the Javascript Date system isn't very malleable when it comes to adding date formats. Here is a reference from Mozilla. I think wierdpanda has the right idea, write a function that accepts your date format, reformats it before feeding it to new Date(), and returns the result. Use this in place of where you have new Date(), and all should be good.

Use JavaScript to convert a date string with timezone to a date object in local time

The format of my date string looks like this: yyyy-MM-ddTHH:mm:ss-0Z00
Example 1: 2010-03-05T07:03:51-0800
Example 2: 2010-07-01T20:23:00-0700
I need to create a date object using these date strings. new Date() does not work on this string.
Please help me convert these date strings into a date objects with the local timezone.
Thank you!
Edit: I am using this in Pentaho Data Integration 4.3.0.
Take my timezone as an example (AEST):
function parseDate(str_date) {
return new Date(Date.parse(str_date));
}
var str_date = "2015-05-01T22:00:00+10:00"; //AEST time
var locale_date = parseDate(str_date);
locale_date: Fri May 01 2015 22:00:00 GMT+1000 (AEST)
var str_date = "2015-05-01T22:00:00+00:00" //UTC time
var locale_date = parseDate(str_date);
locale_date: Sat May 02 2015 08:00:00 GMT+1000 (AEST)
You can use a library such as Moment.js to do this.
See the String + Format parsing.
http://momentjs.com/docs/#/parsing/string-format/
The following should parse your date you provided, but you may need to modify it for your needs.
var oldDate = "2010-03-05T07:03:51-0800";
var dateObj = moment(oldDate, "YYY-MM-DDTHH:mm:ssZ").toDate();
Alternatively, see Moment's String parser, which looks like it is in the format you provided, with the exception of a space between the seconds of the time and the time zone.
http://momentjs.com/docs/#/parsing/string/
Alternative
A second way of doing this is Date.js, another library that seems to parse the format just fine. http://www.datejs.com
Date String:
var strDate = "2010-07-01T20:23:00-0700";
To local time representation in native JS Date object:
var ltzDate = (new Date(strDate)).toLocaleString();

jquery/javascript convert date string to date

I have a date string "Sunday, February 28, 2010" that I would like to convert to a js date object formatted # MM/DD/YYYY but don't know how. Any suggestions?
If you're running with jQuery you can use the datepicker UI library's parseDate function to convert your string to a date:
var d = $.datepicker.parseDate("DD, MM dd, yy", "Sunday, February 28, 2010");
and then follow it up with the formatDate method to get it to the string format you want
var datestrInNewFormat = $.datepicker.formatDate( "mm/dd/yy", d);
If you're not running with jQuery of course its probably not the best plan given you'd need jQuery core as well as the datepicker UI module... best to go with the suggestion from Segfault above to use date.js.
HTH
I would grab date.js or else you will need to roll your own formatting function.
var stringDate = "Sunday, February 28, 2010";
var months = ["January", "February", "March"]; // You add the rest :-)
var m = /(\w+) (\d+), (\d+)/.exec(stringDate);
var date = new Date(+m[3], months.indexOf(m[1]), +m[2]);
The indexOf method on arrays is only supported on newer browsers (i.e. not IE). You'll need to do the searching yourself or use one of the many libraries that provide the same functionality.
Also the code is lacking any error checking which should be added. (String not matching the regular expression, non existent months, etc.)
If you only need it once, it's overkill to load a plugin.
For a date "dd/mm/yyyy", this works for me:
new Date(d.date.substring(6, 10),d.date.substring(3, 5)-1,d.date.substring(0, 2));
Just invert month and day for mm/dd/yyyy, the syntax is new Date(y,m,d)
I used the javascript date funtion toLocaleDateString to get
var Today = new Date();
var r = Today.toLocaleDateString();
The result of r will be
11/29/2016
More info at:
http://www.w3schools.com/jsref/jsref_tolocaledatestring.asp
Use moment js for any date operation.
https://momentjs.com/
console.log(moment("Sunday, February 28, 2010").format('MM/DD/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Categories