I have a string
2010-08-02 12:13:06.0
and need to get something like
Fri Aug 6 2010
out of it (the input does not map to the output for the values I gave, just examples)
I fear Im going to have to do some string manipulation to get what I want; the js Date object does not seem to have methods capable of parsing the input string.
Is this correct?
We are using jquery, but cant find anything in that library that would help...
Everything has been invented before us:
http://www.mattkruse.com/javascript/date/
You can use the date object for this. Just parse the first part of the date string to get the individual numbers and use setFullYear(), setMonth(), setDate(). You will have to subtract 1 from the month, but then use the toDateString() and it outputs it like your example. http://www.w3schools.com/jsref/jsref_obj_date.asp
Related
I'm trying to generate links to Google calendar and see this tool:
https://decomaan.github.io/google-calendar-link-generator/
And the links are generated as:
https://www.google.com/calendar/render?action=TEMPLATE&text=Appointment+with+VULKOVICH%2C+BILL&details=a+Description&location=a+Location&dates=20210105T103300Z%2F20210114T103300Z
and as you can see the dates are like:
20210105T103300Z
and I am trying to convert this to my own dates but I don't know which type is this and how to format. I have the dates both, in moment or in date, but don't know how to convert.
That's ISO-8601
The first part is the date in year-month-date order, the second part is the time and the final letter indicates the timezone (here Z for 'Zulu')
Since you're using moment.js: moment().utc().format('YYYYMMDDTHHmmss[Z]'), or without a library new Date().toISOString().replace(/\W/g,'').replace(/\d{3}Z/,'Z'). This is really a duplicate of How to format a JavaScript date.
Source: comment by RobG Jan 11 at 12:58
I am parsing 2 different date strings
var d1 = '2014-02-01T00:00:00.000+0530'
var d2 = '2014-02-23T00:00:00.000+0530'
when i parse them using moment
alert(moment(d1, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
alert(moment(d2, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
both of them print Sat Feb 1 2014 xxxxx
what is wrong with it??
here is the link to the fiddle i created
jsfiddle
I think your moment formatting string is causing you the problem. If I remove this, the dates do not print as the same.
http://jsfiddle.net/K5ub8/7/
EDIT: The specific issue is you are using dd for day, instead of DD. http://momentjs.com/docs/#/parsing/string-format/
Here is your fiddle fixed:
http://jsfiddle.net/K5ub8/9/
However, I am not 100% sure about the fractional seconds, I believe it is SSS instead of fffffff but I would test this if you need to cater for fractional seconds.
I should mention that if you are converting it back into a JavaScript date object anyway with toDate(), then you don't really need the moment formatting parameter as the date will be formatted in JSON Date format.
I would question why you would want to generate a moment formatted date, and then convert it back to JavaScript, a normal practice might be to receive a date in JavaScript format, then create a moment object which you can use to perform calculations and display in a nice user friendly way.
Simple answer: your format was off a bit.
http://jsfiddle.net/K5ub8/8/
After tweaking the format to be 'YYYY-MM-DDTHH:mm:ss.SSSZZ' rather than 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' it worked just fine. When you're trying to debug issues like this, it's always good to keep the format in a separate variable so you can use the same format that you're trying to parse out to display what you're getting. Had you done that, you would have noticed that 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' was messed up due to it printing out 2014-01-Fr"T"11:32:03.fffffff"-08:00". Which obviously isn't quite right.
For example, I have a string date like this (I'm getting this from the server in json, from rails app)
s = "2013-09-01T00:00:00.000+08:00"
I would like to display it like so
01.09.2013
So I'm using moment.js library for this
moment(s).zone("+08:00").format("DD.MM.YYYY")
>> "01.09.2013"
But I don't know if needed timezone is +08:00. If I skip .zone() call, result would be wrong because my browser is in differnt timezone
moment(s).format("DD.MM.YYYY")
>"31.08.2013"
Even though in my original string I had +08:00 at the end.
So, my question is how can I extract time zone from json date string using pure javascript or moment.js library?
The simplest way I can think of is extracting the last 6 characters manually,
s.slice(s.length - 6, s.length)
> "+08:00"
But maybe there is a better approach for this task?
Just use the parseZone function, like so:
moment.parseZone(s)
Documentation is here.
Alternatively, you can use the older approach, which does the same thing:
moment(s).zone(s)
Consider if we have a date in the format Sat Jul 28 2012 , is there a general a function to convert it in to any wanted format??
say for example 28-07-2012,
deciding the separators like - or /
Javascript's Date object has lots of different versions of toString and different getters, so it should be pretty easy to get the output you want. Scroll down through this documentation to see some of your options. They have pretty good examples too if you click on them.
In addition, the Date constructor is fairly good at taking in most strings and converting it.
var myDate = new Date("Sat Jul 28 2012");
alert(myDate.toLocaleDateString());
Or use the different getters and string concatenation wrapped in a function to make your own.
You can write a function to do it, i dont think there is a Native method:
function convert(dateObj) {
var format = dateObj.getFullYear()+"-";
format += dateObj.getMonth()+"-";
format += dateObj.getDate();
return format;
}
you can customize it however you want. here is the list of methods for the date object
Javascript only outputs it into the standard format you provided above. You can try using the getDate(), getDay(), getMonth() methods (among others) to extract the necessary data and convert it to your liking.
Please refer to W3Schools' description of the JavaScript Date object.
I am using one jquery date picker,
with using picker i am getting date in like this format
Friday, May 21, 2010
Now i want to add one day in this date so i think, i can only do if i change the date in exact format like
21/5/2010
I want to only convert that bcz i want to add one day to the particular date.
So what do u suggest me? How can I do that?
Can i do without converting it ?
thanks in advance....
Take a look at http://docs.jquery.com/UI/Datepicker#option-dateFormat
datejs may be useful to you.
In addition to the formatting options given by others, you should add using date objects rather than to the string representation of the date object.
I.E.
// add 5 days to today
var myDate=new Date();
myDate.setDate(myDate.getDate()+5);
I think you need to detail what jQuery plugin do you use.
Is it this one? http://jqueryui.com/demos/datepicker/
If so, then when you cann getDate method, you'll get Date object representing a date. You can easily manipulate it.
The date format has nothing to do with how dates are stored; it only affects the way dates are displayed. JavaScript has a native Date object and jQuery UI's Datepicker allows to access such object:
http://jqueryui.com/demos/datepicker/#method-getDate
Once you have a Date object, you can alter it to suit your needs:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date
Finally, you can feed it back into Datepicker:
http://jqueryui.com/demos/datepicker/#method-setDate