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/
Related
I'm working in a from who has a date field and by default it shows the current date.
I set the date using this:
var date = new Date(); = Tue May 25 2021 17:06:01 GMT-0600 (Mountain Daylight Time) {}**
Everything works fine, but when I send the data to the controller, the JSON automatically converts it to ISO and the date received by the controller is 6 hours in advance.
I understand a little bit the context about GMT-0006 (my current timezone is 6 hours more than the 0 timezone), and the fact that my controllers received the date in ISO format because when I converted to ISO format is the same problem
date.toISOString() = "2021-05-25T23:06:01.861Z" (6 hours in advance)
so my question is, there is a way to create a date that allows me to use .toISOString() and keep the same?
or create a date with my current hour but -0000 so when I convert it to toISOString keeps the same?
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.
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
I'm trying to hit a soccer sports API which includes date in the format of yyyy-mm-dd, only the scores from that date to current date will be displayed. The current date is chosen by user using a calendar but when the user chooses the date from calendar, it gets displayed in ISO format as "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)" . I want to convert this date in the front end in the yyyy-mm-dd format and send it to the API Url in back end. I'm using AngularJS and Java. How do I convert the full ISO date into that format?
Based on that output it sounds like your date is stored as a JavaScript date object (see: https://www.w3schools.com/js/js_dates.asp)
To get the string you want, one solution would be to take the value of your input (I'll call it d) and do the following (I assume you have momentjs loaded:
var datestring = moment(d).format('YYYY-MM-DD')
datestring should now include the date in the format you want... if for some reason d is a string instead of a date object, you can create a parsing pattern following the momentjs doc here: https://momentjs.com/docs/#/parsing/
Assuming you have a JavaScript Date object to work with, you can do this in plain JS:
var datestring = dateobj.toISOString().substring(0, 10); // 'yyyy-MM-dd'
If you only have the display string ("Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)"), you can first convert that into a Date object with this:
// displaystring = "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)";
var dateobj = new Date(displaystring);
...and then do the datestring conversion above.
I am receiving times in the an AJAX request and am converting them using the new Date() function.
I receive 2013-06-18T12:00:15Z
However, somehow I get the following after new Date():
Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)
Why is it not:
Tue Jun 18 2013 12:00
See the following demo:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_convert
This is a time zone problem. You must be in the EDT timezone (GMT-0400). To correctly parse the date you should tell the parser in which timezone your date is correct.
For you parse your date like this :
new Date('2013-06-18 12:00:15 GMT-0400')
"GMT-0400" means GMT time minus 4 hours
Or if you don't wish to reformat your string, you can use the date.getUTC* functions to get the time as you parsed it.
The full list is available at Mozilla's documentation.
I agree with Vaim Caen's answer that this is a timezone issue, but not with parsing - the date is being parsed fine, but into your local timezone, while you're expecting it to be parsed into UTC date.
This answer shows how to convert from your current timezone to UTC - applying this to the TryIt demo gives:
var msec = Date.parse("2013-06-18T12:00:15Z");
// or: var msec = Date.parse("Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)");
var d = new Date(msec);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
document.getElementById("demo").innerHTML = d;
Edit: If you all you're interested in is displaying the date (no further manipulations) then you can use:
d.toUTCString()
which will show the date in GMT (for me it actually shows "GMT" so most likely not of use!)
The alternative is to add a function to the prototype to show the date in whatever format you want and use the date.getUTC* methods.
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();