ASP.NET convert date from yyyyMMdd to dd MMM yyyy code behind - code-behind

Can you help me how to convert date from yyyyMMdd to dd MMM yyyy with code behind?
I have date value 20151012, I need to convert to 12 Oct 2015.
Thank you

public Datetime dateVariable{get;set;}
public string strVariable {get;set;}
strVariable=dateVariable.tostring("DD mmm yyyy");

Related

What is this datetimestamp 20220914171900Z-0700 in JSON?

I see below datetimestamp in json but not sure which format is this??
20220914171900Z-0700
any suggestions??
Thanks in advance!
I used constructDateString but no luck!
2022 - yyyy (Year)
09 - MM (Month)
14 - dd (day of month)
17 - HH (Hour of day)
19 - mm (Minute of hour)
00 - ss (Second of minute)
Z-0700 - Z stands for Zulu and represents 00:00 hours offset from UTC. So, Z-0700 means an offset of 07:00 hours from UTC. If you want to get this date-time at UTC, you can do so by adding 07:00 hours to 2022-09-14T17:19.
Demo using Java:
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMddHHmmss'Z'Z");
String strDateTime = "20220914171900Z-0700";
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtf);
System.out.println(odt);
// The same date-time at UTC
OffsetDateTime odtUTC = odt.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(odtUTC);
}
}
Output:
2022-09-14T17:19-07:00
2022-09-15T00:19Z
ONLINE DEMO
Note: You can use y instead of u here but I prefer u to y.
Learn more about the modern Date-Time API from Trail: Date Time.

Converting Month Number(Date Time or 4 byte integer) to Month Name(String) SSIS

I need to convert month number to month name.
I have date time as the date type - 2009-01-01 00:00:00.000
I also have 4-byte integer data type - 1
how do I convert this 1 to "January" for example?
i think you are in the data flow:
it is really easy to get MOnth Name in a script component from Date:
add a varchar column to your dataflow
Mark your date column for read access
enter the following script
Row.[NewColumnName] = Row.[Your Date Column].ToString("MMMM");
Result:
Here is a good translations for any date part to string formatting:
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
Furthermore, you asked about quarters. I don't think it is as easy but here is something I stole from another answer.
Build DateTime extensions:
Normal Quarter:
public static int GetQuarter(this DateTime date)
{
return (date.Month + 2)/3;
}
Financial Year Quarter (This case is for quarters that start on April 1):
public static int GetFinancialQuarter(this DateTime date)
{
return (date.AddMonths(-3).Month + 2)/3;
}
Integer division will truncate decimals, giving you an integer result. Place methods into a static class and you will have an extension method to be used as follows:
Row.calendarQuarter = Row.[your Date Column].GetQuarter()
Row.fiscalQuarter = Row.[your Date Column].GetFinancialQuarter()
In SQL Server, one method is:
select datename(month, datefromparts(2000, 1, 1))
The first "1" is the column for the month. The year is arbitrary.
following steps:
create variable with datetime datatype and assigned value.
used MONTH function in ssis to extract month number and assigned to new variable with integer data type: #[User::newdata]= MONTH( #[User::dbdate])
finally used if else condition which manually compare all 12 months
(code available:1)

To convert the json date which is coming from solr to actual date (yyyy/mm/dd)

Var idate =res.results[r].date
date is coming from solr
The above line output is in the format
Mon Apr 22 14:49:00 2019
I have tried code but I am getting today's date I want the date which is coming from solr below is the code
Var idate2=new Date(idate)//idate I am passing which is coming from solr.....
Var n=idate2.Tolocaledatestring();
Console.log(n);
Output I am getting is 5/5/2019 but I want 22/5/2019.
Thanks
you can use this code for convert your date:
var date = new Date('Mon Apr 22 14:49:00 2019');
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
var result = day + '/' + month + '/' + year; // output is 22/3/2019

Swift 3 parsing date string

I am trying to parse a string I return from a MySQL Date datatype as a date locally. However everytime I try to parse it with a DateFormatter() in Swift 3, the result date is two days off.
Here is an example of the date string returned from the server:
"Sat Dec 31 2016 00:00:00 GMT-0800 (PST)"
I try to use the DateFormatter() to capture that information in the following format string:
let DatFormatServerTwo = "EEE MMM dd yyyy HH':'mm':'ss zzzZ '('zzz')'"
Then I use it like this:
static func stringDateToDateTwo(dateString: String, timeZone: TimeZone = TimeZone.current) -> Date {
dateFormatter.dateFormat = DateFormatServerTwo
dateFormatter.timeZone = timeZone
return dateFormatter.date(from: dateString) ?? Date()
}
where dateFormatter is assigned to a DateFormatter()
I think the problem is that there is an offset with the timezone and I am not capturing that information properly. I get the desired date by chopping off parts of the date string namely as soon as the TimeZone stuff enters into the picture. I don't want to do that everytime though because it is messy.
Here is how I chop the string to get the date that I want from the server:
var holidayDateArray = holidayDate.characters.split{$0 == " "}.map(String.init)
var count = 0
var newString = ""
for substring in holidayDateArray {
if count < 5 {
newString.append(substring)
} else {
break
}
count += 1
}
And then I format like this:
static let DateFormatServerTwo = "EEEMMMddyyyyHH':'mm':'ss"
One issue is there is no format specifier for a timezone in the format GMT-XXXX. There is one for GMT-XX:XX but you don't have that format. So this solution is to treat the GMT as a literal and just parse the -XXXX part using the Z specifier. The end result is the same.
Also note there is no need to quote punctuation, just letters that are to be treated literally. There is also no need to set the formatter's timezone since you will be getting timezone info from the date string.
There is no need to process the string at all. Just use the correct format:
let str = "Sat Dec 31 2016 00:00:00 GMT-0800 (PST)"
let fmt = DateFormatter()
fmt.dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)"
let dt = fmt.date(from:str)
This gives the correct result for dt for the given string.
Try the following in a Playground:
let str = "Sat Dec 31 2016 00:00:00 GMT-0800 (PST)"
let fmt = DateFormatter()
fmt.dateFormat = "EEE MMM dd yyyy HH':'mm':'ss zzzZ '('zzz')'"
let dt = fmt.date(from:str)
You will notice that the date is nil.
If you remove the extra timezone information from the date string, and format the date formatter string accordingly:
let str = "Sat Dec 31 2016 00:00:00 -0800"
let fmt = DateFormatter()
fmt.dateFormat = "EEE MMM dd yyyy HH':'mm':'ss ZZZZ"
let dt = fmt.date(from:str)
You'll get: Dec 31, 2016, 1:30 PM
Incidentally, you don't need the quotes around the colons in the date format. So you can actually have the date format as:
fmt.dateFormat = "EEE MMM dd yyyy HH:mm:ss ZZZZ"
If the date string is consistent in how the date is laid out, you can easily remove the GMT and time zone within the quotes by doing something like:
let str = "Sat Dec 31 2016 00:00:00 GMT-0800 (PST)"
let newStr = str.replacingOccurrences(of:"GMT", with:"").replacingOccurrences(of:"\\(.*?\\)", with:"", options: String.CompareOptions.regularExpression)
let fmt = DateFormatter()
fmt.dateFormat = "EEE MMM dd yyyy HH:mm:ss ZZZZ"
let dt = fmt.date(from:newStr)
When done this way, the date does appear to come through correctly. Let me know if your results are different when you try this in a Playground.

How to get MMM dd yyyy hh:mm:ss format in sql server 2008

I want to get datetime as Jan 17 2013 4:34:59 with time in 24 hrs format
SELECT CONVERT(VARCHAR(12), SYSDATETIME(), 107) +' '+ CONVERT(VARCHAR(12), SYSDATETIME(), 108) AS [Mon DD, YYYY]