I am using following code to format my datum
ToString("MMMM dd, hh:mmtt", System.Globalization.CultureInfo.CreateSpecificCulture("en-UK")
It results in i.e. June 01, 8:34PM. I need only "PM" ("AM") be lowercase "pm" ("am"). What is the simplest method to achieve it?
Thanks!
I think you're going to have to split the strings and join them together.
Also, you don't need to specify the culture if you're doing custom formatting. The culture is really for the standard formats.
So I would do something like the following
var curDate = DateTime.Now;
var dateString = curDate.ToString("MMMM dd, hh:mm") + curDate.ToString("tt").ToLower();
Try this
DateTime dt = new DateTime(2013, 3, 9, 16, 5, 7, 123); // put your datetime variable value at last paramter value.
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
For lowercase you just need to use .ToLower().
Or you can find more information HERE
Related
I need convert GMT time 14:21:34 to minute (14*60+21=861 minutes)
I know there is a inbuilt function which convert min to HH:MM
Use TimeSpan.FromMinutes:
var result = TimeSpan.FromMinutes(1815);
Is there an easy way to do this that I am missing?
Two options to get to a TimeSpan:
1) Parse it as a DateTime (specifying a format), then use DateTime.TimeOfDay to get a TimeSpan
var dt = DateTime.ParseExact(
"14:21:34",
#"HH\:mm\:ss",
CultureInfo.InvariantCulture);
var ts = dt.TimeOfDay;
I like this option as it sounds like the value you've got is meant to be a time of day.
2) Parse it as a TimeSpan:
var ts = TimeSpan.ParseExact(
"14:21:34",
#"hh\:mm\:ss",
CultureInfo.InvariantCulture);
Once you have a TimeSpan, use TotalMinutes (which returns a double) to get the number of minutes. For example:
Console.WriteLine((int) ts.TotalMinutes); // 861
I need help with a quick question I a string "07/10/2014" how can I get first the year "2014",second the month "10" ,third the day- "07" with out "/" only the values in VB.NET
Please show me the full way how to do it.
First declare it like this Dim x as Date = "07/10/2014". And to get the individual values use x.Day, x.Month and x.Year
Use the DateTime.Parse() method then use the return DateTime structure to extract the Month, Day, Year properties (similar to DJK's answer above).
If the thread's current culture is set to one that understands "mm/dd/yyyy" format, then the code can be as simple as:
Dim dt As DateTime = DateTime.Parse("07/15/2014")
MessageBox.Show(String.Format("Month: {0}; Day: {1}; Year: {2}", dt.Month, dt.Day, dt.Year))
Have a look at this.
Dim MyDate As Date
MyDate = "07/10/2014"
MsgBox(Format(MyDate, "dd")) ' dd gives you day number
MsgBox(Format(MyDate, "MM")) ' MM gives you month number
MsgBox(Format(MyDate, "YYYY")) ' YYYY gives you year number
The full list of date fomatting string could be found here (MSDN)
UPDATE
Use following example to assign to a string variable
Dim DayOfString As String DayOfString
DayOfString = Format(MyDate, "dd")
I have
Datetime starttime='1/1/1900 6:00:00PM'
Datetime ExamDate='12/9/2013 12:00:00PM'
i want to combine these such that the result
Datetime combine=starttime+ExamDate;
so,
Result Datetime combine='12/9/2013 6:00:00PM'
Please help thank you
I'm not 100% certain if this is what you're trying to achieve, but I'll give it a shot. To add time to an existing DateTime object, use the TimeSpan class. Ex:
TimeSpan StartTime = new TimeSpan(0, 6, 0, 0); //new time span of 6 hours
// create date time 2013-09-12 12:00
DateTime ExamDate = new DateTime(2013, 9, 12, 12);
//Add the 6 hour time span to your exam date to get combined date
DateTime Combined = ExamDate + StartTime;
If you'd simply like to add the TIME portion of starttime to examDate
var startTime= DateTime.Parse("1/1/1900 6:00:00PM");
var examDate = DateTime.Parse("12/9/2013 12:00:00PM");
var result = examTime.Add(starttime.TimeOfDay);
This will result in: 13/9/2013 6:00:00AM.
It's occurred to me you might just be trying to store the date in examDate, in which case setting examDate to 12/9/2013 12:00:00AM will give: 12/9/2013 6:00:00PM.
I'm trying to convert a string into a date format
My string looks like this
Dim MyString as String = "June 2011"
And I'm trying to convert it like this
Convert.ToDateTime(MyString).ToString("MM yyyy")
But it's giving me the error
Syntax error converting datetime from
character string.
The desired output would be either 06/2011 or 01/06/2011. The value of my string will only ever have the month and year in though.
Any ideas?
A datetime object in .NET represents one single point in time. To create one from a Gregorian calendar date, you need all three parts (day, month, year), otherwise it can't represent one single point in time.
Since your business requirement is to assume that the day number is 1 if not provided, just insert that number into the string before parsing.
Dim myDate As Date = Convert.ToDateTime("1 " & MyString)
EDIT:
Sorry, forgot to mention that string manipulations like that are of course culture-dependent. You don't mention what culture you are in. "June 2011" can imply either en-GB or en-US. Since inserting the day number at the start of the string is easier than trying to insert it between the month and year, I suggest you go for something like this.
Dim myDate1 As Date = Date.Parse("1 " & myString, CultureInfo.GetCultureInfo("en-GB"), DateTimeStyles.AllowWhiteSpaces)
Convert.ToDateTime() only takes the full date so try prepending 01 as the day first i.e.
Convert.ToDateTime("01 " + MyString)
DateTime.Parse(MyString).ToString("MM yyyy")
This worked for me
Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
Dim MyString As String
Dim d As DateTime = DateTime.Now 'test start
'test all Month Year strings
For x As Integer = 1 To 12
MyString = d.ToString("MMMM yyyy") 'convert date to March 2011 then April 2011 then...
Dim dt As DateTime = DateTime.ParseExact(MyString, "MMMM yyyy", provider)
Debug.WriteLine(MyString & " " & dt.ToString)
d = d.AddMonths(1)
Next
I need to retrieve the current date in asp.net and then compare that with the date given by the user in textbox1.text(mm/dd/yyyy format), if date date given is greater than current date then error else add 4months2days with that date and display it in textbox2.text.
help me please,
thanking you guys,
Indranil
DateTime dateToCompare;
if(DateTime.TryParse(textbox1.text, out dateToCompare))
{
DateTime current = DateTime.Now;
TimeSpan ts = current - dateToCompare;
if (ts.Ticks < 0)
{
//display error
}
else
textbox2.text = dateToCompare.AddMonths(4).AddDays(2).ToString("mm/dd/yyyy");
}
}
I'm not going to write your code, but in .NET you can use ToString to specify a date format, TryParse to get a date out of a string. And AddDays, AddMonths etc to manipulate a date.
In javascript, there's no simple way to format output, but you can use getMonth etc to prompt the individual values and concatenate a string from that. You can use a combination of getDate and setDate to manipulate dates. It automatically corrects for new months, i.e. if you run myDate.setDate( myDate.getDate() + 60 ) it'll actually increment by 60 days; you won't end up with a weird date like May 74th.
Keep in mind that months in javascript are zero-based, ie January is 0, February is 1, etc.
You can create a new date in javascript by new Date(yy, mm, dd) or new Date('yy/mm/dd'), so you could string-manipulate an input and create a date from that.
To compare two dates, you can subtract one from the other, and get the difference in milliseconds.
if ( dateA - dateB < 0 ) // dateB is greater than dateA (occurrs later)
and
var diff = Math.abs(dateA - dateB) // difference in ms, no matter which date is greater
DateTime date1 = new DateTime();
if(DateTime.TryParse(textbox1.text, out date1)){
if (date1.CompareTo(DateTime.Now) > 0)
{
//Error code here
}else
{
textbox2.text = date1.AddMonths(4).AddDays(2);
}
}