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.
Related
I am getting Time stamp as "2020-03-02T05:50:31.000Z"
How to convert this to Normal Readable Format with Date and Time Zone
const parsedDate = new Date("2020-03-02T05:50:31.000Z");
console.log(parsedDate.toGMTString())
//"Mon, 02 Mar 2020 05:50:31 GMT"
console.log(parsedDate.toLocaleString())
//"3/2/2020, 11:20:31 AM"
console.log(parsedDate.toDateString(), parsedDate.toTimeString())
//Mon Mar 02 2020 11:20:31 GMT+0530 (India Standard Time)
In java-script above date format can be parsed by using Date.
Eg:
var date = new Date('2020-03-02T05:50:31.000Z');
I don't know what you mean readable format but you can use following methods:
new Date('2020-03-02T05:50:31.000Z').toLocaleString();
// outputs date according to user locale settings
Or you can use getYear, getMonth, getDay methods to get them and format date as you want
You can use moment.js for date time format and conversion.
Pass your date timestamp as below:
moment.parseZone("2020-03-02T05:50:31.000Z").format("DD-MM-YYYY hh:mm:ss z Z")
Modify format as you need ref mentioned here
Use in your code as mentioned here
I'm using moment() and want to know if there's a shortcut to give me the same date string as new Date() would give me.
I have to do some timezone conversions so using moment.js makes that easier, but I need the same date format: Thu Oct 06 2016 23:08:53 GMT-0700 (PDT) as the native Date object would give me.
I would assume there is a shortcut for this, but I can't find it.
moment.tz('America/New_York').format('???')
I could not find a shortcut however this gives the string I need:
moment.tz('America/New_York').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (z)')
from the docs: http://momentjs.com/docs/#/displaying/
Returns: Fri Oct 07 2016 02:25:17 GMT-0400 (EDT)
if you have no idea which timezone the browser is currently in, you can try moment.tz.guess(), it isn't 100% correct for timezone offset that has multiple names though:
var d = moment()._d;
d.toString().replace(/\([\w ]+\)$/, moment.tz(d, moment.tz.guess()).format('(z)'));
The date format you're referring to, depends on the culture/language of your browser. That aside, if you want the same format as the standard new Date().toString() returns, you just use the toString() method on the moment object, without any arguments:
moment().toString();
Source: http://momentjs.com/docs/#/displaying/as-string/
How can I create a Date object from a date with this format:
03/23/2016 02:00:00 PM
The Date object can parse strings: new Date('03/23/2016 02:00:00 PM')
For example:
var date = new Date('03/23/2016 02:00:00 PM') // => "Wed Mar 23 2016 14:00:00 GMT-0700 (PDT)"
date.getFullYear() // => 2016
However, I would recommend using a library that someone else has already spent time considering the edge cases, like time zones, etc. (a good one I've used is moment.js).
Keep in mind (from the moment.js docs):
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
var date = new Date("3/23/2016 02:00:00 PM");
console.log(date);
You can then access all the methods of the Date object.
Looking at MDN
var date = new Date('03/23/2016 02:00:00 PM')
You can use something like date.js:
First use script, then write date:
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
....
document.write(new Date().toString("dd:MM:yyyy hh:mm:ss tt"));
my datetime format is given below.
var datetime =Wed Apr 8 15:05:00 UTC+0530 2015;
I only want to get the time 15.05
Use format() and specify the desired token.
moment().format('HH.mm').
Docs for tokens can be found here
You need to create moment object from your string. Then append .format()
var date = moment("Wed Apr 8 15:05:00 UTC+0530 2015").format('HH.mm');
alert(date);
var date = moment("Wed Apr 8 15:05:00 2015").format('HH.mm');
alert(date);
If you want to get "15.05", you will need to remove the "UTC+0530", because its modifying your time :)
You can see examples here:
http://momentjs.com/docs/#/displaying/format/
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();